0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Laravelでデータ削除時に子供テーブルも削除する

Posted at

以下を使えば自動で子供テーブルを削除したり、追加のロジックを実行できる。

オブザーバーを登録するには、監視するモデルでobserveメソッドを呼び出す必要があります。アプリケーションのApp\Providers\EventServiceProviderサービスプロバイダのbootメソッドにオブザーバーを登録できます。
オブザーバーがリッスンできる他のイベントには、savingやretrievedなどがあります。

xxx.php
    protected static function boot()
    {
        parent::boot();

        static::deleting(function ($xxx) {
            $xxx->user->delete();
            $xxx->aaa()->delete();
            //他にやりたいことがあれば
        });
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?