php - Automatically deleting related rows in Laravel (Eloquent ORM)

ID : 20192

viewed : 25

Tags : phplaraveleloquenteloquent-relationshipphp

Top 5 Answer for php - Automatically deleting related rows in Laravel (Eloquent ORM)

vote vote

91

I believe this is a perfect use-case for Eloquent events (http://laravel.com/docs/eloquent#model-events). You can use the "deleting" event to do the cleanup:

class User extends Eloquent {     public function photos()     {         return $this->has_many('Photo');     }      // this is a recommended way to declare event handlers     public static function boot() {         parent::boot();          static::deleting(function($user) { // before delete() method call this              $user->photos()->delete();              // do the rest of the cleanup...         });     } } 

You should probably also put the whole thing inside a transaction, to ensure the referential integrity..

vote vote

80

You can actually set this up in your migrations:

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');

Source: http://laravel.com/docs/5.1/migrations#foreign-key-constraints

You may also specify the desired action for the "on delete" and "on update" properties of the constraint:

$table->foreign('user_id')       ->references('id')->on('users')       ->onDelete('cascade'); 
vote vote

76

Note: This answer was written for Laravel 3. Thus might or might not works well in more recent version of Laravel.

You can delete all related photos before actually deleting the user.

<?php  class User extends Eloquent {      public function photos()     {         return $this->has_many('Photo');     }      public function delete()     {         // delete all related photos          $this->photos()->delete();         // as suggested by Dirk in comment,         // it's an uglier alternative, but faster         // Photo::where("user_id", $this->id)->delete()          // delete the user         return parent::delete();     } } 

Hope it helps.

vote vote

65

Relation in User model:

public function photos() {     return $this->hasMany('Photo'); } 

Delete record and related:

$user = User::find($id);  // delete related    $user->photos()->delete();  $user->delete(); 
vote vote

51

There are 3 approaches to solving this:

1. Using Eloquent Events On Model Boot (ref: https://laravel.com/docs/5.7/eloquent#events)

class User extends Eloquent {     public static function boot() {         parent::boot();          static::deleting(function($user) {              $user->photos()->delete();         });     } } 

2. Using Eloquent Event Observers (ref: https://laravel.com/docs/5.7/eloquent#observers)

In your AppServiceProvider, register the observer like so:

public function boot() {     User::observe(UserObserver::class); } 

Next, add an Observer class like so:

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

3. Using Foreign Key Constraints (ref: https://laravel.com/docs/5.7/migrations#foreign-key-constraints)

$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); 

Top 3 video Explaining php - Automatically deleting related rows in Laravel (Eloquent ORM)

Related QUESTION?