[php] Laravel back button

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?

This question is related to php laravel

The answer is


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.


One of the below solve your problem

URL::previous() 
URL::back()

other

URL::current()

On 5.1 I could only get this to work.

<a href="{{ URL::previous() }}" class="btn btn-default">Back</a>

<a href="{{ url()->previous() }}" class="btn btn-warning"><i class="fa fa-angle-left"></i> Continue Shopping</a>

This worked in Laravel 5.8


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.


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.


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)


You can use javascript for this provblem. It's retrieve link from browser history.

_x000D_
_x000D_
<script>_x000D_
function goBack() {_x000D_
  window.history.back();_x000D_
}_x000D_
</script>
_x000D_
<button onclick="goBack()">Go Back</button>
_x000D_
_x000D_
_x000D_


Laravel 5.2+, back button

<a href="{{ url()->previous() }}" class="btn btn-default">Back</a>