0

I am trying to delete all related rows in Laravel with:

$customers = Customer::find($clientId);
$customers->delete();
$customers->locations()->delete();
$customers->locations->objects()->delete();
$customers->locations->objects->subscriptions()->delete();
$customers->locations->objects->history()->delete();

And also tried:

$customers = Customer::find($clientId);
$customers->delete();
$customers->locations()->delete();
$customers->locations()->objects()->delete();
$customers->locations()->objects()->subscriptions()->delete();
$customers->locations()->objects()->history()->delete();

Laravel deletes the customer and locations but does not delete the objects, subscriptions and history and trows an error.

What can I do to delete them too?

EDIT: I changed the order like this:

$customers = Customer::find($clientId);
$customers->locations()->objects()->subscriptions()->delete();
$customers->locations()->objects()->history()->delete();
$customers->locations()->objects()->delete();
$customers->locations()->delete();
$customers->delete();

and get the error Call to undefined method Illuminate\Database\Query\Builder::objects()

5
  • have you tried deleting related model first and then parent model? Commented Sep 24, 2016 at 8:34
  • @Anik I use soft deletes, it shouldn't be a problem Commented Sep 24, 2016 at 8:35
  • what error did it throws? Commented Sep 24, 2016 at 8:39
  • I think that would be problem, because when you do $customers->locations()->delete() it tries to find out location of customers which are not deleted (in your case, it use deleted_at is NULL) and deleted_at is not NULL when you already deleted customer. Commented Sep 24, 2016 at 8:42
  • @AgeDeO Can you confirm if you have added objects function as relation in location model? Commented Sep 24, 2016 at 8:46

3 Answers 3

2

you need to override delete method in model to delete all related objects for each location and also delete should be in order, so first delete objects, then locations and then customer. for ex.

$customers = Customer::find($clientId);
$customers->locations()->delete();

now for delete location override delete method in you model something like

class Location extends Model {
     public function delete() {
       $this->objects()->delete();
       parent::delete();
     }
}

//also delete hierarchy in your object model with overriding delete method

class Object extends Model {
     public function delete(){
        $this->history()->delete();
        $this->subscriptions()->delete();
        parent::delete();
     }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Just to add information, this would be only good if you want to delete all related models every time parent model is deleted.
if its not the case then change the method name with some custom name for example deleteReferences() so it won't behave as default always delete method.
0

Most likely to do in the reverse order.
If you removed the "parent", the "children", he can not find.
Try this.

$customers = Customer::find($clientId);
$customers->locations()->objects()->subscriptions()->delete();
$customers->locations()->objects()->history()->delete();
$customers->locations()->objects()->delete();
$customers->locations()->delete();
$customers->delete();

Comments

0

When you say

$customers->locations()->delete()

you are trying to delete a Collection. You should delete a Model.

So get all the locations using

$locations = $customer->locations

and then use a foreach loop to delete each location.

foreach($locations as $location) {
  $location->delete();
}

Similarly delete all other related models.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.