Search code examples
laravelobservers

laravel : in observer how to fired detach method for pivot table?


I implemented 3 table :product , shop and product_shop for pivot.

in the pivot table I have 4 column: id , product_id , shop_id , product_count.

and I made an observer for ProductShop model , but the deleted method not fired !

should I create detached method for pivot observer ?


Solution

  • I created a foreach on $shop->products and detach one by one pivot rows, it's work! and deleted method fired correctly.

    foreach($shop->products as $product){
            $shop->products()->detach($product->id);
        }