yes, but as @supersan pointed out in the comment above, if you delete () in QueryBuilder, the model event will not be fired because we do not load the model itself and then call delete () for this model.
Events are triggered only if we use the delete function in the model instance.
So this being said:
if user->hasMany(post) and if post->hasMany(tags)
to remove the tags $user->posts when deleting a user, we will need to iterate over $user->posts and call $post->delete()
foreach($user->posts as $post) { $post->delete(); } foreach($user->posts as $post) { $post->delete(); } foreach($user->posts as $post) { $post->delete(); } foreach($user->posts as $post) { $post->delete(); } → this will trigger a delete event in the post
V.S.
$user->posts()->delete() → this will not trigger a message delete event because we are not actually loading the message model (we only run SQL- DELETE * from posts where user_id = $user->id : DELETE * from posts where user_id = $user->id and thus the Post model doesn’t even load)
rechim Sep 24 '18 at 12:38 2018-09-24 12:38
source share