Support the ongoing development of Laravel.io →
Eloquent Queues Laravel

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();

return response()->json($result);
Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.