In your function just pass id
public function restore(Post $post,$id)
{
$post->onlyTrashed()->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash',$post->id);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
i made a soft delete in laravel and i want to restore the delete post, but it return 404 not found, the data are exist in database with delete_at value, can somebody help me?, here my code Route
Route::put('/blog/post/{post}/restore',[
'uses' => 'Backend\BlogController@restore',
'as' => 'post.restore'
]);
Restore Function
public function restore(Post $post)
{
$post->onlyTrashed()->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash');
}
Restore button
{!! Form::open(['style' => 'display:inline-block','method' => 'PUT', 'route' => ['post.restore', $post->id]])!!}
<button title="Restore Post" type="submit" class="btn btn-xs btn-default">
<i class="fa fa-refresh"></i>
</button>
{!! Form::close() !!}
In your function just pass id
public function restore(Post $post,$id)
{
$post->onlyTrashed()->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash',$post->id);
}
Route model binding returns 404 if you route to a soft deleted element.
public function restore($postId) {
\App\Post::onlyTrashed()->findOrFail($postId)->restore();
}
@MUNAZZIL - i mean when i dd the $post is null
Can you show below command result?
php artisan route:list
@ORION - when i edit it it show me an error it say Too few arguments to function App\Http\Controllers\Backend\BlogController::restore(), 0 passed and exactly 1 expected
@MUNAZZIL - +--------+-----------+--------------------------------+---------------------+------------------------------------------------------------------------+------------------------------+ | Domain | Method | URI | Name | Action | Middleware | +--------+-----------+--------------------------------+---------------------+------------------------------------------------------------------------+------------------------------+ | | GET|HEAD | / | blog | App\Http\Controllers\BlogController@index | web | | | GET|HEAD | api/user | | Closure | api,auth:api | | | GET|HEAD | author/{author} | author | App\Http\Controllers\BlogController@author | web | | | GET|HEAD | blog/post | post.index | App\Http\Controllers\Backend\BlogController@index | web,auth | | | POST | blog/post | post.store | App\Http\Controllers\Backend\BlogController@store | web,auth | | | GET|HEAD | blog/post/create | post.create | App\Http\Controllers\Backend\BlogController@create | web,auth | | | DELETE | blog/post/{post} | post.destroy | App\Http\Controllers\Backend\BlogController@destroy | web,auth | | | PUT|PATCH | blog/post/{post} | post.update | App\Http\Controllers\Backend\BlogController@update | web,auth | | | GET|HEAD | blog/post/{post} | post.show | App\Http\Controllers\Backend\BlogController@show | web,auth | | | GET|HEAD | blog/post/{post}/edit | post.edit | App\Http\Controllers\Backend\BlogController@edit | web,auth | | | DELETE | blog/post/{post}/force-destroy | post.force-destroy | App\Http\Controllers\Backend\BlogController@forceDestroy | web,auth | | | PUT | blog/post/{post}/restore | post.restore | App\Http\Controllers\Backend\BlogController@restore | web,auth | | | GET|HEAD | category/{category} | category | App\Http\Controllers\BlogController@category | web | | | GET|HEAD | email/resend | verification.resend | App\Http\Controllers\Auth\VerificationController@resend | web,auth,throttle:6,1 | | | GET|HEAD | email/verify | verification.notice | App\Http\Controllers\Auth\VerificationController@show | web,auth | | | GET|HEAD | email/verify/{id} | verification.verify | App\Http\Controllers\Auth\VerificationController@verify | web,auth,signed,throttle:6,1 | | | GET|HEAD | home | home | App\Http\Controllers\Backend\HomeController@index | web,auth | | | GET|HEAD | login | login | App\Http\Controllers\Auth\LoginController@showLoginForm | web,guest | | | POST | login | | App\Http\Controllers\Auth\LoginController@login | web,guest | | | POST | logout | logout | App\Http\Controllers\Auth\LoginController@logout | web | | | POST | password/email | password.email | App\Http\Controllers\Auth\ForgotPasswordController@sendResetLinkEmail | web,guest | | | POST | password/reset | password.update | App\Http\Controllers\Auth\ResetPasswordController@reset | web,guest | | | GET|HEAD | password/reset | password.request | App\Http\Controllers\Auth\ForgotPasswordController@showLinkRequestForm | web,guest | | | GET|HEAD | password/reset/{token} | password.reset | App\Http\Controllers\Auth\ResetPasswordController@showResetForm | web,guest | | | GET|HEAD | {post} | blog.single | App\Http\Controllers\BlogController@single | web | +--------+-----------+--------------------------------+---------------------+------------------------------------------------------------------------+------------------------------+
Use as like below one i have remove first / because of route error,
Route::put('blog/post/{post}/restore',[
'uses' => 'Backend\BlogController@restore',
'as' => 'post.restore'
]);
@MUNAZZIL - it also return 404
I think you haven't pass $post to the form
public function restore(Post $post)
{
$post->onlyTrashed()->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash',compact('post'));
}
@MUNAZZIL - still have the same error, it return not found
Try as like below because onlyTrashed it won't work
public function restore(Post $post,$id)
{
$post = Post::findOrFail($id);
$post->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash');
}
@MUNAZZIL - also won't work
Route::post('/blog/post/{post}/restore',[
'uses' => 'Backend\BlogController@restore',
'as' => 'post.restore'
]);
Method should be post.
{!! Form::open(['style' => 'display:inline-block','method' => 'POST', 'route' => ['post.restore',$post->id]])!!}
And also try as like below as well
Route::resource('/blog/post/{post}/restore',[
'uses' => 'Backend\BlogController',
'as' => 'post.restore'
]);
@NOMGUY - still have same error
another way i try this one
public function restore(Post $slug)
{
$slug->withTrashed()->restore();
alert()->success('post has remove from trash')->persistent('Ok');
return redirect('/blog/post?status=trash');
}
but it restore all the post in the trash, not the specific post, any solution to make it restore only specific post?
Use first(),
$slug->withTrashed()->restore()->first();
@MUNAZZIL - no, its not work
Then pass slug id,
return redirect('/blog/post?status=trash',$slug->id);
you need to pass the id of the post so that it could pick that one only..
Your code has to be like:
{!! Form::open(['style' => 'display:inline-block','method' => 'PUT', 'route' => ['post.restore', $post->id]])!!}
<button title="Restore Post" type="submit" class="btn btn-xs btn-default">
<i class="fa fa-refresh"></i>
</button>
{!! Form::close() !!}
Route::post('/blog/post/{post_id}/restore',[
'uses' => 'Backend\BlogController@restore',
'as' => 'post.restore'
]);
public function restore($post_id)
{
$post = Post::find($post_id);
$post->onlyTrashed()->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash');
}
Or else you can print the request too. to know the data
public function restore(Request $request, $post_id)
{
echo '<pre>';
print_r($request->all());
die;
$post = Post::find($post_id);
$post->onlyTrashed()->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash');
}
@NOMGUY - it show error like this Too few arguments to function App\Http\Controllers\Backend\BlogController::restore(), 0 passed and exactly 1 expected
@MUNAZZIL - it say Argument 2 passed to Symfony\Component\HttpFoundation\RedirectResponse::__construct() must be of the type int, null given,
yes because it needs an 'id' to run restore function. Until or unless you pass an id to specify a post, it will show that error again and again.
Do it like this. It will work for you.
public function restore($post_id)
{
Post::withTrashed()->find($post_id)->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash');
}
It's going to work @chairuman
You should try this:
Your route like
Route::get('/blog/post/{post}/restore',[
'uses' => 'Backend\BlogController@restore',
'as' => 'post.restore'
]);
Your function like
public function restore($postId)
{
Post::withTrashed()->find($postId)->restore();
alert()->success('post has been restore')->persistent('Ok');
return redirect('/blog/post?status=trash');
}
Your view like this
{!! Form::open(['style' => 'display:inline-block','method' => 'GET', 'route' => ['post.restore', $post->id]])!!}
<button title="Restore Post" type="submit" class="btn btn-xs btn-default">
<i class="fa fa-refresh"></i>
</button>
{!! Form::close() !!}
Use as like below
public function restore(Post $post,$slug)
Please or to participate in this conversation.