When using Laravel, you may sometimes create list screens that span dozens of pages. In such cases, there may be a requirement to move to an edit screen and, after completing the update, return to the list screen at the exact pagination state you were previously viewing.
In this article, we introduce a concrete method to meet that requirement.

Route Definitions

First, set up the routes for the list screen and the edit screen. Below is a concrete example.

// List screen
Route::get('/employees', 'EmployeesController@index');

// Edit screen
Route::get('/employeesedit/{employees}','EmployeesController@edit');
Route::post('/employeesedit/{employees}','EmployeesController@edit');

// Update process
Route::post('/employees/update','EmployeesController@update');

Controller Code to Get the Previous Page URL and Display the Edit Screen

Next, we introduce how to get the previous page URL when displaying the edit screen.
In this example, data with a matching ID is retrieved from the employee table (employees), and the previous page URL is obtained using url()->previous().
If the previous page URL is not the list screen (for example, due to a page refresh), the list screen URL is specified directly.

    // Edit screen
    public function edit($id)
    {
        $employees = Employee::find($id);

        $prevurl = url()->previous();	// Get the previous page URL

        // If the previous page URL is not the list screen (with parameters)
        if(false === strpos($prevurl, 'employees?')){
            $prevurl = url('/employees');	// Directly specify the list screen URL
        }

        return view('employeesedit', [
            'employee' => $employees,
            'prevurl' => $prevurl,	// URL for the “Back to List” button on the edit screen
        ]);
    }

View Code for the Edit Screen to Set the Previous Page URL

Next, we explain how to configure the view that displays the edit screen.
Below is a concrete example of a Blade template (employeesedit.blade.php) for the edit screen.
By setting the URL {{$prevurl}} on the “Back to List” button, you can link back to the previous page.
If the previous page URL is not the list screen due to a page refresh or other reasons, the user will be redirected to the list screen URL.

<div>
    @include('common.errors')
</div>

<form action="{{ url('employees/update') }}" method="POST">
    @csrf

   <div>
	Edit Screen: ID “<b>{{$employee->id}}</b>”
	<input type="hidden" name="id" value="{{$employee->id}}">
   </div>

   <div>
   <label for="employee_name">Employee Name</label>
   <input type="text" name="employee_name" value="{{ old('employee_name', $employee->employee_name) }}">
   </div>

   <div>
   <label for="employee_department">Department Name</label>
   <input type="text" name="employee_department" value="{{ old('employee_department', $employee->employee_department) }}">
   </div>

    <button type="submit">Update</button>

    <div>
    <a class="btn btn-link" href="{{$prevurl}}">
       < Back to List Screen
    </a>
    </div>
</form>

Summary

As shown above, you can achieve smooth navigation from a list screen to an edit screen using Laravel.
Even when dealing with a large number of pages, you can easily implement a function to return to the previous page, significantly improving user convenience.

Use this approach to create more user-friendly web applications.
We hope it will be helpful in your future projects.

 
*If you reuse this content, please do so at your own responsibility.*