So I have 2 Models Users & Staff. They have one to one relationship with one another.
User.php 'hasOne' Staff AND Staff.php 'belongsTo' User
When I Soft Delete a User I want to soft delete Staff entry as well, I have achieved this using(Works Perfectly):
static::deleting(function ($user) {
$user->staff()->delete();
});
Now I want to restore the staff when I restore the User for that I have tried using this(Not working):
static::restoring(function ($user) {
$user->staff()->restore();
});
But this is not working. The User Entry is deleted but the Staff entry still remains soft deleted.
Can someone help me understand what I am doing wrong here?
Also, Is this the best way to get this done? Or is there some other way this should be done?
PS: I'm using Laravel 5.5
It isn't working because $user->staff() doesn't fetch deleted staff. That's how relationships work by default.
Just replace it with this:
static::restoring(function ($user) {
$user->staff()->withTrashed()->restore();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With