berkayk
151
4
General

Querying models with contains

Posted 4 years ago by berkayk

Hi all, I am trying to implement a blog where posts are being commented and liked. I have defined relations such that a post has many likes(or comments) and a like(or comment) belongs to a post etc. Everything is working fine but I need to find a way whether a post is liked by a user or not. In the controller, before liking a post, I have iterated through all likes of the post and checked for the author_id. This is working fine but I couldn't find a good way to put these on the view side.

$post->likes->contains(Auth::user()) doesn't work because contains expects the key of the like.

Currently, I am looping through all likes and trying to find a match with the user id. Is this the optimized way of doing it or am I missing something?

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.