Laravel back button
PhpLaravelPhp Problem Overview
I am trying to create a simple back button on a page. The user can arrive to this page from two different pages so I would like to know from which page he arrived. Is that possible?
Php Solutions
Solution 1 - Php
In Laravel, you can do something like this: <a href="{{ Request::referrer() }}">Back</a>
(assuming you're using Blade).
Laravel 4
{{ URL::previous() }}
Laravel 5+
{{ url()->previous() }}
Solution 2 - Php
I know this is an oldish question but I found it whilst looking for the same solution. The solution above doesn't appear to work in Laravel 4, you can however use this now:
<a href="{{ URL::previous() }}">Go Back</a>
Hope this helps people who look for this feature in L4
(Source: https://github.com/laravel/framework/pull/501/commits)
Solution 3 - Php
Laravel 5.2+, back button
<a href="{{ url()->previous() }}" class="btn btn-default">Back</a>
Solution 4 - Php
Indeed using {{ URL:previous() }}
do work, but if you're using a same named route to display multiple views, it will take you back to the first endpoint of this route.
In my case, I have a named route, which based on a parameter selected by the user, can render 3 different views. Of course, I have a default case for the first enter in this route, when the user doesn't selected any option yet.
When I use URL:previous()
, Laravel take me back to the default view, even if the user has selected some other option. Only using javascript inside the button I accomplished to be returned to the correct view:
<a href="javascript:history.back()" class="btn btn-default">Voltar</a>
I'm tested this on Laravel 5.3, just for clarification.
Solution 5 - Php
The following is a complete Blade (the templating engine Laravel uses) solution:
{!! link_to(URL::previous(), 'Cancel', ['class' => 'btn btn-default']) !!}
The options array with the class is optional, in this case it specifies the styling for a Bootstrap 3 button.
Solution 6 - Php
On 5.1 I could only get this to work.
<a href="{{ URL::previous() }}" class="btn btn-default">Back</a>
Solution 7 - Php
You can use javascript for this provblem. It's retrieve link from browser history.
<script>
function goBack() {
window.history.back();
}
</script>
<button onclick="goBack()">Go Back</button>
Solution 8 - Php
One of the below solve your problem
URL::previous()
URL::back()
other
URL::current()
Solution 9 - Php
You can use {{ URL::previous() }} But it not perfect UX.
For example, when you press F5 button and click again to Back Button with {{ URL::previous() }} you will stay in.
A good way is using {{ route('page.edit', $item->id) }} it always true page you wanna to redirect.
Solution 10 - Php
<a href="{{ url()->previous() }}" class="btn btn-warning"><i class="fa fa-angle-left"></i> Continue Shopping</a>
This worked in Laravel 5.8