where() will return a collection, try this instead:
User::withTrashed()->find($id)->restore()
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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>
where() will return a collection, try this instead:
User::withTrashed()->find($id)->restore()
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();
@nakov after using find($id), getting this error
Symfony\Component\Debug\Exception\FatalThrowableError
Call to a member function restore() on null
@sinnbeck same error happens
So the user with that ID does not exists at all.
@nakov So How to fix this..?
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 :)
@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.
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...
try this @neeraj1005
$userdata = User::onlyTrashed()->find($id);
if (!is_null($userdata)) {
$userdata->restore();
}
hey its possible check my answer and try this
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');
}
@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.
@nakov you are right sir
@neeraj1005 please check are you implement soft delete
try this link to learn how to soft delete
https://medium.com/@chrissoemma/laravel-5-8-delete-and-soft-delete-practical-examples-b9b71c0a97f
@van1310 Nothing is happen. No error comes ony page redirect.
@neeraj1005 show me your delete code
@nakov No, user was not deleted before SoftDelete implemented. Yes soft delete add a timestamp deleted_at in my users table.
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'));
}
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();
}
@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.
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
@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>
ok try this
$userdata = User::onlyTrashed()->findorFail($id);
or
https://medium.com/@chrissoemma/laravel-5-8-delete-and-soft-delete-practical-examples-b9b71c0a97f
try follow this link its step step to check your code may be this help you
@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>
@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');
}
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');
}
@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
Great. Thats the error. Change the url (use the correct id)
<li><a href="{{ url('admin/users/restore/{{$user->id}}') }}">Restore</a></li>
Please or to participate in this conversation.