Search code examples
laravelurllaravel-8laravel-routing

Return To Previous Page - Laravel 8 Project


I have a dashboard that views categories using a paginator and therefore my url often looks something like: http://127.0.0.1:8000/categories?page=1 on my category.view page.

Viewing List of Categories

When I click Edit it takes me to the category edit page.

enter image description here

On this page I have a go back to categories button.

View Categories

That View Categories button works fine if the edit category page was not refreshed/reloaded.

Here's the code for my View Categories button

<a href="{{ url()->previous() }}" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a>

The problem I have is that if I was to refresh the Edit Category page or save some edits this View Categories button stops to work as intended because when I click it just refreshes and stay on the same Edit Category page.

I know that that is caused by the fact that in my browser history once Edit Category page is reloaded it becomes the last page in history and therefore will rightfully be the one to load.

How do I make my View Categories button takes me back to the last paginated page even after reloading/refreshing the current Edit Category page?

I've used : <a href="{{ url()->previous() }}" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a>

<a href="{{ URL::previous() }}" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a>

<a href="history.back()" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a> and none of them works as intended.

I can make the View Categories button to go back to the categories page but the problem with that I have to make sure that it lands on the last visited http://127.0.0.1:8000/categories?page=NUMBER

Please Note : This is not a Controller, Component, Routing or web.php problem. All of that works fine. I just want my Go Back button to work well.


Solution

  • This was very useful Laravel Session

      public function index(Request $request){
            $categories = Category::latest()->paginate(5);
            //the following snippet creates persistent session variables
            $request->session()->put('page',$categories->currentPage());
            return view('orders.view',compact('orders'));
      }
    

    Also changed the View Categories button from this:

    <a href="{{ url()->previous() }}" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a>

    To this:

    <a href="{{ Session::has('page') ? '/categories?page=' . Session::get('page') : '/categories' }}" class="btn btn-primary"><i class="material-icons">arrow_back</i> View Categories</a>

    This button first tests if $_SESSION['page'] was set and if so when clicked it assigned the last known value to ?page=, as extra precaution, if for whatever reason the session value was not found the button will just redirect back to category.view which is the categories landing page.

    This is exactly what I wanted to achieve and I was surprised to find that nothing native or built-in method exists because I've checked most public forums and re-checked the Laravel Doc and found nothing to what I'm looking for. Maybe the question was just not clear enough but I am sure I've provided enough material to give a clear picture of what I wanted to achieve.