F
Filament6mo ago
EMMAN

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.
Pastebin
Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.
No description
11 Replies
toeknee
toeknee6mo ago
Is the advise record soft deleted? If so you'll need to get it first with onlyTrashed or withTrashed
EMMAN
EMMANOP6mo ago
yes. its have soft deleted
toeknee
toeknee6mo ago
So you need to ensure you ->withTrashed() it
EMMAN
EMMANOP6mo ago
so instead of '$adviser->user->restore();' i need to use the '$adviser->user->withTrashed();' ? am i right?
toeknee
toeknee6mo ago
$adviser->user->withTrashed()->restore();
EMMAN
EMMANOP6mo ago
thank you sir, but same problem
No description
toeknee
toeknee6mo ago
You need to apply the withTrashed() to the if statement too.
EMMAN
EMMANOP6mo ago
ERROR 😦
No description
No description
EMMAN
EMMANOP6mo ago
can someone help me guys, thank you.
Dennis Koch
Dennis Koch6mo ago
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()
EMMAN
EMMANOP6mo ago
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); } });
Want results from more Discord servers?
Add your server