murilo

Select Query Posts with Likes

Posted 8 months ago by murilo

Hello , I have a question , I am developing a website like a facebook . It will have POSTS and a User can LIKE those posts ,

So a user can search all those posts , if this post is LIKED ( this user has given a like ) , so it has to show that this post is liked .

The like and POST is related by Many to MAny relation ship .

Will be like that -

POST MODEL

 // LIKE
    public function Like() {
        return $this->belongsToMany(User::class , 'user_post_likeble' , 'post_id'  , 'user_id');
    }


POST QUERY RESULT ->


  $result  = Prop::select(['id', 'title' , 'description'])

    // LIKED , IT SAYS TRUE OR FALSE
            ->with([ 'Like' => function($query) {
                // I dont know how could I make the relation ship here to say TRUE for those one that has relation chip / FALSE for those one that dont has relation ship .                 

            } ])->get()



Thanks .

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