ryrobbo
ryrobbo

Reputation: 83

Laravel restore trashed not working

The restore function looks like it is working up until it saves its state in the Database. I have this code:

if($result->trashed()) {
     Log::info($result->deleted_at);

     $result->restore();

     Log::info($result->deleted_at);
}

$result->award_id = 1;
$result->save();

Log::info($result->deleted_at);

The debugs show the code working how it should like so:

2015-09-22
null
null

However, the record in the database is still showing a date in the deleted_at column.

I'm on Laravel 4.2.

Upvotes: 1

Views: 1656

Answers (1)

ryrobbo
ryrobbo

Reputation: 83

It turns out that this is a pretty major bug with Laravel 4.2 as per the issue raised by myself on Github:

https://github.com/laravel/framework/issues/10630#issuecomment-148739682

Unfortunately, 4.2 is no longer supported so I had to replace the restore() method with:

UserClass::where('user_id', '=', $user->id)
                ->where('class_id', '=', $class_id)
                ->withTrashed()
                ->update([
                      'deleted_at' => null
                ]);

As far as I know, this is still an issue with later versions of Laravel.

Upvotes: 1

Related Questions