Automatically deleting related rows in Laravel (Eloquent ORM)

PHOTO EMBED

Wed Mar 23 2022 03:02:28 GMT+0000 (UTC)

Saved by @Jaskaran #laravel #php

public function boot()
{
    User::observe(UserObserver::class);
}
Next, add an Observer class like so:

class UserObserver
{
    public function deleting(User $user)
    {
         $user->photos()->delete();
    }
}
content_copyCOPY

https://gist.github.com/dev-jaskaranSingh/e209225626d7b1b18a89d41d4a6ecfed