Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Neeraj1005's avatar

SoftDelete Restore specific users

I want to restore the specific user. But when I click Restore link nothing is happen. Can anyone tell where did I mistake? Here is my controller function for restoring

    public function userRestore($id)
    {
        $userdata = User::withTrashed()
                        ->where('id', $id)
                        ->restore();
        return redirect('admin/users/trash')->with('User', 'Restored Successfully');
    }

This is my route

Route::get('admin/users/trash','Admin\ListUserController@userTrash')->name('user.trash');//User Trash
Route::get('admin/users/restore/{id}','Admin\ListUserController@userRestore');

And this is my view Delete

<ul class="dropdown-menu" role="menu">                                              
                     <li><a href="{{ url('admin/users/restore/{id}') }}">Restore</a></li>
                               </ul>
0 likes
37 replies
Nakov's avatar

where() will return a collection, try this instead:

User::withTrashed()->find($id)->restore()
Sinnbeck's avatar

I expect that only one user has that id? (sorry forgot the withTrashed(). I see @nakov have it so go with his) :)

User::find($id)->restore();
Neeraj1005's avatar

@nakov after using find($id), getting this error

Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function restore() on null
Nakov's avatar

So the user with that ID does not exists at all.

Nakov's avatar

Create a user, delete it, and try to restore it using the option that me and sinnebeck shared above. To fix completely deleted user, you should have a backup of the database.. no other way :)

Neeraj1005's avatar

@nakov But If i put this code in function

    public function userRestore()
    {
        $userdata = User::withTrashed()->restore();
        return redirect('admin/users/trash')->with('User', 'Restored Successfully');
    }

All user will be restored. Basically I have used the Laravel SoftDelete method.

Nakov's avatar

Yes, all the user that exists and were deleted. But clearly, the user with the $id that you are passing cannot be found in your database. You might have force deleted it, or deleted it manually, check your database...

vandan's avatar

try this @neeraj1005

$userdata = User::onlyTrashed()->find($id);

if (!is_null($userdata)) {
            $userdata->restore();
}
vandan's avatar

hey its possible check my answer and try this

Neeraj1005's avatar

Nothing happens @van1310

    public function userRestore($id)
    {
        // $userdata = User::withTrashed()->restore();
        // return redirect('admin/users/trash')->with('User', 'Restored Successfully');
        $userdata = User::onlyTrashed()->find($id);

        if (!is_null($userdata)) {
            
        $userdata->restore();
        }

        return redirect('admin/users/trash')->with('User', 'Restored Successfully');
    }
1 like
Nakov's avatar

@neeraj1005 you cannot restore something that does not exists at all. Did you deleted the user before you implemented the SoftDelete? If so, then there is no way back except for a database backup if you kept one. What soft delete does is just adds a timestamp in a deleted_at field. Find the user with the ID that you are trying to restore in your DB manually, if it is there, then something else is wrong. If it is gone, then it is gone.. only BACKUP can save you, that's all.

1 like
Neeraj1005's avatar

@nakov No, user was not deleted before SoftDelete implemented. Yes soft delete add a timestamp deleted_at in my users table.

Neeraj1005's avatar

@van1310

   public function delete($id)//for deleting 
    {
        $userdata = User::find($id);
        $userdata->delete();
        return redirect('admin/user-list')->with('success', 'Deleted Successfully...!');
    }

    public function userTrash()//for trashed 
    {
        $userdata = User::onlyTrashed()->get();
        $total = count($userdata);
        return view('admin.trash.userbin', compact('userdata','total'));
    }
1 like
vandan's avatar

@neeraj1005

your softdelete record in database or not?

or else migrate refresh and try

this code

$userdata = User::onlyTrashed()->find($id);

if (!is_null($userdata)) {
            $userdata->restore();
}
Neeraj1005's avatar

@van1310 yes, data is in database.

    public function userRestore($id)
    {
        $userdata = User::onlyTrashed()->find($id);

        if (!is_null($userdata)) {
            $userdata->restore();
        }
        return redirect('admin/users/trash')->with('User', 'Restored Successfully');
    }

Again nothing happens.

1 like
vandan's avatar

@neeraj1005

then check your route its passing proper id in route

check your route and also blade file via pass id or show me your blade and route file

i think id not proper passing show me blade and route file

Neeraj1005's avatar

@van1310 Route

Route::get('admin/users/trash','Admin\ListUserController@userTrash')->name('user.trash');//User Trash
Route::get('admin/users/restore/{id}','Admin\ListUserController@userRestore');

Blade

<ul class="dropdown-menu" role="menu">                                              
                     <li><a href="{{ url('admin/users/restore/{id}') }}">Restore</a></li>
                               </ul>
1 like
Neeraj1005's avatar

@van1310 Now I am getting blank page. with this address url

http://127.0.0.1:8000/admin/users/restore/%7Bid%7D

This is my route

<li><a href="{{ url('admin/users/restore/{id}') }}">Restore</a></li>
Neeraj1005's avatar

@van1310 @nakov @sinnbeck when I put the specific Id of users, It restore the users. So, Instead of given user ID, What dynamic value should I have to put.

   public function userRestore($id)
    {
        $userdata = User::onlyTrashed()->where('id',12)->restore();
        return redirect('admin/users/trash')->with('User', 'Restored Successfully');
    }
Sinnbeck's avatar

Try checking if you are getting the expected id

public function userRestore($id)
    {
        dd($id); //is this 12?
        $userdata = User::onlyTrashed()->where('id',12)->restore();
        return redirect('admin/users/trash')->with('User', 'Restored Successfully');
    }
1 like
Neeraj1005's avatar

@sinnbeck after using dd($id) this return to me

"{id}"

this screen And the address url is

http://127.0.0.1:8000/admin/users/restore/%7Bid%7D
Sinnbeck's avatar

Great. Thats the error. Change the url (use the correct id)

<li><a href="{{ url('admin/users/restore/{{$user->id}}') }}">Restore</a></li>
2 likes
Next

Please or to participate in this conversation.