Home > Net >  Laravel url previous function getting base link instead of previous
Laravel url previous function getting base link instead of previous

Time:10-04

I am making use of Laravel 9

This is a very weird issue and I really don't know what could be causing it. I am trying to implement a 404 error page(.../errors/404.blade.php) where I have a link that says Go back as in go back to the previous page you came from.

Here is the code I tried: <a href="{{ url()->previous() }}" >Go back</a>

Instead of linking to the previous page, it rather links me to the home page (127.0.0.1:8000). Could it be that the url() helper function does not work on error pages or I am doing something wrong?

CodePudding user response:

Never used the url()->previous(); but you can try this one. I implemented same thing you are doing in an old site of mine

  back()->getTargetUrl();

enter image description here

CodePudding user response:

After searching far and wide and trying so many things, I discovered that it just does not work on the error page. I even tried routing to another page instead of the previous page but it still linked me to the home page 127.0.0.1:8000

So the solution I found was to make use of javascript instead. On the link that says Go Back, I added a history.back() function to it on click. Works perfectly and achieves what I desired. So my link now looks like this instead:

<a onclick="history.back()" href="javascript:void" >Go back</a>

  • Related