Search code examples
laraveleloquentmany-to-manywherehas

Laravel eloquent many to many relation deep conditions


I have two Model named Product and Category. Admin can change the status of Product and Category. Here Product and Category has many to many relations. So that, all Product can belongs to multiple Category

Now at my user panel, I want to show that all Product whose all Category's status is active.


Solution

  • It works for me, got all the products that have all the categories active. First time I thought complicated so I didn't solve.

    $products = Product::whereDoesntHave('catagories',function($category){
        $category->where('status','!=',1);
    });