Using Laravel, I have the following code
$review = Review::find(1);
$review->delete();
Review
has a many to many relationship defined with a Product
entity. When I delete a review, I'd expect it to be detached from the associated products in the pivot table, but this isn't the case. When I run the code above, I still see the linking row in the pivot table.
Have I missed something out here or is this the way Laravel works? I'm aware of the detach()
method, but I thought that deleting an entity would also detach it from any related entities automatically.
Review
is defined like this:
<?php
class Review extends Eloquent
{
public function products()
{
return $this->belongsToMany('Product');
}
}
Product
is defined like this:
<?php
class Product extends Eloquent
{
public function reviews()
{
return $this->belongsToMany('Review');
}
}
Thanks in advance.
Eloquent
works. You can use DB events for the pivot table (on delete cascade
) or implement your event handlers using Eloquent. Something like stackoverflow.com/a/14174356/784588 – Jarek Tkaczyk Dec 6 '14 at 10:44