Automatically deleting related rows in Laravel (Eloquent ORM)

后端 未结 13 1950
半阙折子戏
半阙折子戏 2020-11-22 17:03

When I delete a row using this syntax:

$user->delete();

Is there a way to attach a callback of sorts, so that it would e.g. do this auto

13条回答
  •  感动是毒
    2020-11-22 17:56

    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');
    

提交回复
热议问题