Skip to content
Advertisement

Show post comments with relationship – Laravel

Hi I am new to Laravel and I and trying to create a relationship bewteen my posts and post comments.

I want to show all comments on a post with the same post_id as the posts id. I think it should look something like this. But I can not get it to work. Any suggestions ?

In the AppBlogComments.php

JavaScript

In the AppPost.php

JavaScript

In the BlogCommentsController:

JavaScript

In the posts/show.blade.php

JavaScript

Advertisement

Answer

In controller use this

JavaScript

Or if only need blogComments that case

JavaScript

Or you can do it this way

JavaScript

in view

JavaScript

Edit: for comment issue

In BlogComments model define

JavaScript

And usage

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement