0
votes

I wonder why I am unable to restore soft deleted data in laravel. I have did everything right but not sure what I am missing so I cannot able to restore data.

My model is

namespace App\Model\Clients;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class SocialAccounts extends Model{

   protected $table = 'social_accounts';
   use SoftDeletes;
   protected $dates = ['deleted_at'];
}

I have used soft delete traits. For restore my soft deleted data I am doing something like below

$restoreAccount = SocialAccounts::withTrashed()->find($id)->restore();

but It doesn't restore data, I am assuming when we restore data laravel NULL the deleted_at column, but it is not updating anything on that id

I have also tried

//second alternate method 
$restoreAccount= SocialAccounts::withTrashed()->find($id)->update(['deleted_at' => NULL]);

//Third alternate method
$restoreAccount = SocialAccounts::withTrashed()->find($id); 
$restoreAccount->deleted_at = NULL;
$restoreAccount->save();

I am not sure what wrong I am doing, may there is anything we have to do to achieve restore? I checked official laravel doc and follow the same.

2
Are you sure something else in the request is not deleting it? i.e. it's getting restored but then deleted again? - Ben Swinburne
I manage to sort it out. I was using the DB transaction and without committing it I was checking for the result. Silly me. Thanks for all your helps. - Yamin

2 Answers

1
votes

I dont know if you already find the answer or not, but maybe you can try this in your controller

public function restore($id) 
{
    $restoreAccount = SocialAccounts::onlyTrashed()->->where('id', $id)->restore();
    return redirect()->route('your.route');
}
0
votes

$restoreAccount = SocialAccounts::onlyTrashed()->find($id)->restore();