I am using Laravel Commentable that uses Baum
Post Model
class Post extends Model
{
use Commentable;
....
My user model name is User
Comment table structure:
The user_id
stores the user id of the user who commented, commentable_id
stores the post id of the post where the comment was placed.
The Comments are working as expected.
I am able to insert comments, delete comments.
To display the comments:
$comments = Comment::orderBy('id', 'desc')->get();
@foreach($comments as $comment)
{{ $comment->user->name }} : {{ $comment->body }}
@endforeach
This gives me the name and the comment by the user in the view.
Question: How to get the post model attributes from a comment?
{{ $comment->post->slug }}
<-this doesn't works
Per the code from this package, you have to use the commentable
relation. However, there is no way to be sure that this will be a Post
model, this can be any model that is commentable.
An example that checks if the commentable is actualy a post object and shows the slug if so:
@if ($comment->commentable_type === \App\Post::class)
{{ $comment->commentable->slug }}
@endif