Support the ongoing development of Laravel.io →
Database Eloquent

I have a typical event hook for soft deleting children:

public static function boot()
{
	parent::boot();
	static::deleted(function($model1)
	{
		$model1->hasmanyrelation()->delete();
	});
}

and

public function hasmanyrelation()
{
	return $this->hasMany('Model2');
}

Now when I use:

$model0->model1->each(function($model1){$model1->delete();});

things work as expected and model2 children of model 1 get (soft) deleted. But when I use:

$model0->model1()->delete();

then all related model1 records get deleted but model2 and all its records remain untouched. What is wrong with the latter, simpler syntax that it deletes model1 records but skips its mode events??

Last updated 2 years ago.
0

A little confusing what does the model1() method do?

model1()->delete()

$model1->delete()

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

silverdr silverdr Joined 10 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.