Ad

How To Get Post Which Have Multiple Table Relationship?

my database scheema

Table POST

| id | title | content |

Table SharedGroup

|id | title | slug 

Table Post_SharedGroup

|id| post_id | shared_group_id|

i want to make a query in laravel that i give slug of sharedgroup and get post of it

Ad

Answer

$data = Post::whereHas('sharedGroup',function($query) use($slug){
        $query->whereIn('slug',$slug);
       })->get();

where $slug is your slug array given by you

Model Post

public function sharedGroup(){
    return $this->belongsToMany('App\SharedGroup');
}

Model SharedGroup

public function post(){
    return $this->belongsToMany('App\Post');
}
Ad
source: stackoverflow.com
Ad