Restore not working on Relationship
Hello, i have this method on Advisers.php (https://pastebin.com/MUGep5sJ) that updates the other table. UPDATE & DELETE method is working, But RESTORE is not working.
Logging result is:
[2024-06-05 13:45:09] RESTORED event triggered for Adviser ID: 64
Can someone help me. TY.
11 Replies
Is the advise record soft deleted? If so you'll need to get it first with onlyTrashed or withTrashed
yes. its have soft deleted
So you need to ensure you ->withTrashed() it
so instead of '$adviser->user->restore();' i need to use the '$adviser->user->withTrashed();' ? am i right?
$adviser->user->withTrashed()->restore();
thank you sir, but same problem
You need to apply the withTrashed() to the if statement too.
ERROR 😦
can someone help me guys, thank you.
You can’t apply the method on the model (which is null) but need to use it on the DB level:
->user()->withTrashed()->get()->restore()
Thanks sir. That works
static::restored(function ($adviser) {
\Log::info("RESTORED event triggered for Adviser ID: " . $adviser->id);
if ($adviser->user()->withTrashed()->exists()) { $adviser->user()->withTrashed()->restore(); \Log::info("User restored successfully for Adviser ID: " . $adviser->id); } });
if ($adviser->user()->withTrashed()->exists()) { $adviser->user()->withTrashed()->restore(); \Log::info("User restored successfully for Adviser ID: " . $adviser->id); } });