cosapostolo
cosapostolo

Reputation: 1129

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?

Upvotes: 104

Views: 182780

Answers (10)

Anon
Anon

Reputation: 1942

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() }}

Laravel documentation

Upvotes: 191

davidkihara
davidkihara

Reputation: 533

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

This worked in Laravel 5.8

Upvotes: 1

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.

Upvotes: 0

Sohanur Rahman
Sohanur Rahman

Reputation: 49

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>

Upvotes: 4

user9747893
user9747893

Reputation: 29

One of the below solve your problem

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

other

URL::current()

Upvotes: 2

Thiago Cardoso
Thiago Cardoso

Reputation: 542

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.

Upvotes: 28

Akshay Khale
Akshay Khale

Reputation: 8371

Laravel 5.2+, back button

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

Upvotes: 59

mhankins
mhankins

Reputation: 141

On 5.1 I could only get this to work.

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

Upvotes: 14

mike.bronner
mike.bronner

Reputation: 1249

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.

Upvotes: 17

Josh Griggs
Josh Griggs

Reputation: 918

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)

Upvotes: 72

Related Questions