Latest

Tuesday, July 11, 2017

Not able to restore soft deleted data in laravel

Asked by: Yamin


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.


Answers

Answered by: Kuldeep Mishra at 2017-07-11 02:43PM



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




Source

No comments:

Post a Comment

Adbox