mercredi 15 septembre 2021

how to apply filter on pivot table??facing some issue

Model of Category

public function lists(){
        return $this->belongsToMany(listing::class);
    }

Model of Listing

public function categories(){
    return $this->belongsToMany(Category::class)->withPivot(['category_id','listing_id']); }

Controller: when i die dumb this query then i get all lists that is in listings table but i just want to get those lists whose have these categories

 if($request->categories){
            $var=$request->categories;//data in array 1,2,3
            $lists =Listing::with(['categories' => function ($query) use($var)  {
                $query->whereIn('category_id',['=',$var]);

            }])->get();
            dd($lists);//


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire