laravelista / comments

Native comments for your Laravel application.
MIT License
745 stars 144 forks source link

Question: show limit number of comments in blade #89

Closed topegret closed 4 years ago

topegret commented 4 years ago

instead of pagination, how to show limit number of comments , say the lastest 10 comments with their replies in a blade page. Thanks! I quickly look at the "commentable.php" and "commenter.php" in the source file folder but didn't figure it out.

Thanks!

mabasic commented 4 years ago

There are many ways of doing this. Use comments() from the Commentable trait to get the comments for a single model.

The trick is that the comments method returns all comments (parent and replies in a flat array) for a single model. Use whereNull('child_id') to obtain "parent" comments and limit them to 10. Then write a second condition on the query where you load comments which have the ID of parent comments in the `child_id') column.

That would get you latest 10 comments (ofc don't forget to order by created_at) and their replies which you could then manually display or use provided component to show.

You would have to use ->where(function($query) {}) and inside that where (first condition) and orWhere (second condition). See the documentation for advanced where queries. Hope this helps, let me know if you manage to do it.

topegret commented 4 years ago

thanks for the tips.