Home > Blockchain >  $payment->users()->delete() didn't run deleted func in PaymentUserObserver in laravel
$payment->users()->delete() didn't run deleted func in PaymentUserObserver in laravel

Time:08-02

I have a payment that it has many users I want to run deleted function in observer after deleting each user but the function didn't call! users are payment's pivot table.

Payment Observer deleting function

$payment->users()->delete();

PaymentUser observer deleted function

dd($paymentUser);

it didn't work with this code too

$payment->users()->get()->each->delete()

CodePudding user response:

$payment->users()->delete() doesn't pass thru the observers because it runs a SQL delete. It should be:

$payment->users()->get()->each->delete();

CodePudding user response:

Migration: $table->unsignedInteger('example')->primary() Or $table->id();

Model: protected $primaryKey = "example";

Code To Run Observer Method: $payment->users()->get()->each->delete();

It Run Delete for Users One By One & It Trigger The Observer Deleted & Deleting Function.

  • Related