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()