Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: How to redirect after controller method execution

web.php:

Route::post('caption/{id}/delete', 'DetailController@deleteCaption');

DetailController.php:

public function deleteCaption(Request $request, $id) {
    $caption = Caption::findOrFail($id);
    $caption->delete(); //doesn't delete permanently

    return response(204);
}

admin.blade.php:

<p value='{{$caption->id}}'>{{$caption->content}}</p>
<form action="caption/{{$caption->id}}/delete" method="post">
<button type="submit">Delete caption</button>
</form> 
<form action="caption/{{$caption->id}}/approve" method="post">
<button type="submit">Accept caption</button>
</form>     

I want to make it so that after I delete an image, the user is redirected back to the admin page, located at localhost:8000/admin.

How can I do this? Documentation isn't understandable to me.

like image 531
Sahand Avatar asked Oct 26 '25 07:10

Sahand


2 Answers

You can redirect like

public function deleteCaption(Request $request, $id) {
    $caption = Caption::findOrFail($id);
    $caption->delete(); //doesn't delete permanently

    return redirect()->to('link/to/anywhere');
}

OR
You can redirect like this

return redirect()->back();

to your last state.

OR

return route('yourRouteName');
//if there's parameters
return route('yourRouteName', ['id' => 1]);
like image 133
Zugor Avatar answered Oct 27 '25 20:10

Zugor


You can simply redirect to your defined route in your web.php:

public function deleteCaption(Request $request, $id) {
    $caption = Caption::findOrFail($id);
    $caption->delete(); //doesn't delete permanently

    return redirect('admin');
}

https://laravel.com/docs/5.4/responses#redirects

Checking out the routing and blade docs may assist as well.

https://laravel.com/docs/5.4/routing

https://laravel.com/docs/5.4/blade

like image 21
jeremykenedy Avatar answered Oct 27 '25 22:10

jeremykenedy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!