Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nazar1987's avatar

laravel URL::previous

hi every one i try use URL::previous() in back button to help me return to previous URL the problem appear when the validation work the back button still return to the same location and not return me to the previous URL is there solve or i save previous URL in session ??

0 likes
24 replies
Mo7sin's avatar

This is normal, If validation doesn't pass, Previous url will be your current url, can you show us your code please?.

1 like
bobbybouwmann's avatar

The previous function always uses that last requested url. So if you post a form and it fails then the last url is updated to that!

bashy's avatar

That is one long sentence! Not sure I understood what is/isn't working but show some code and examples of what happens...

bashy's avatar

3x ~ or ` at the start and end of the code.

nazar1987's avatar

in the controller

        if ($validator->fails())
        {
            return Redirect::back()->withErrors($validator);
        }

and in the edit view i try use this

<a class="btn btn-info" href="{{ URL::previous() }}">back</a>
1 like
pmall's avatar

So it is perfectly normal it gets you back to the form when the previous url is the form.

Why do you want to implement this ? It seems useless to me...

If you want to allow the user to easily get back to the list of items or to the page of the item being edited, just put a link to this page.

Mo7sin's avatar
<a class="btn btn-info" href="{{ URL::previous() }}">back</a>

Back to where? index view?

1 like
nazar1987's avatar

@Mo7sin yes the back must back me to the index page with page number or other parameter but when invalid data inserted and validation work when user want back to the index page the back button not work

Mo7sin's avatar

You can use route instead of previous

<a class="btn btn-info" href="{{ URL::route('WHATEVER.index') }}">back</a>

Now it will take the user to the index view no matter what.

khaledSMQ's avatar

i think you should use


<a class="btn btn-info" href="{!! URL::previous() !!}">back</a>

// {{ URL::previous() }} it will echo the previous URL , if you use !! it will process the method URL::previous()  and return the URL
Mo7sin's avatar

You can pass an array as second argument as your parameters

<a class="btn btn-info" href="{{ URL::route('WHATEVER.index', []) }}">back</a>
nazar1987's avatar

@Mo7sin i used this to save previous URL in the controller

      if (!Session::has('edit_previous_button'))
      {
        Session::put('edit_previous_button', URL::previous());
      }

and in the view

<a class="btn btn-info" href="{{ Session::get('edit_previous_button') }}">back</a>
1 like
khaledSMQ's avatar

previous didn't accept any variables unless u use the way @Mo7sin mention to pass the parameters through router or action method

 /**
     * Get the URL for the previous request.
     *
     * @return string
     */
    public function previous()
    {
        $referrer = $this->request->headers->get('referer');

        $url = $referrer ? $this->to($referrer) : $this->getPreviousUrlFromSession();

        return $url ?: $this->to('/');
    }
khaledSMQ's avatar

you can also use

<a class="btn btn-info" href="{!! URL::previous() !!}/blabla=bla&blabla=bla">back</a>
// just make sure to add the parameter after }
nazar1987's avatar

@khaledsmq @Mo7sin i solve the problem by use the session to save previous URL because if use URL::previous and when invalid data inserted the back button it refer to the current URL

vghanghas's avatar

How can we go back in the history. Like for eg. if a user already exist in db then I want to go back in history so the filled data remain filled?

Snapey's avatar

@vghanghas ask your own question please. Not drag up something that is 7 years old.

Please or to participate in this conversation.