TeamPlar Jarj
TeamPlar Jarj

Reputation: 121

Browser Back Button Laravel

So I have this back buttons I use this when I create a building then theres a back button for the homepage for edit also and its working.

The problem is I have an OfficePage which have create and edit also, the back button for office page is not working every time I click the create office when I press Back it doesnt refresh the page I have to refresh it so I can see the office that I created heres my routes and code for it.

This is the back button code for building

<a href="{{route('index')}}" class="btn btn-default btn-md"> GO Back</a>

This is the back button code for office

<a href="javascript:history.back()" class="btn btn-default">Back</a> 

Routes

Route::get('/home', 'HomeController@index')->name('home');

Route::get('/', 'BuildingController@index')->name('index');

Route::get('building/{id}', 'PageController@show');

Route::get('buildings/create', 'BuildingController@create')->name('createbform')

Route::post('building/create/store', 'BuildingController@store')->name('createbuilding');

Route::get('building/{id}/edit', 'BuildingController@edit');

Route::post('building/{id}/edit', 'BuildingController@update')->name('editbuilding');

Route::get('building/{id}/delete', 'BuildingController@destroy');

Route::get('office/{id}', 'OfficeController@show')->name('officeMenu');

Route::get('building/{id}/offices/create', 'OfficeController@create')->name('createofficeform');

Route::post('building/{id}/offices/create/store', 'OfficeController@store')->name('createoffice');

Route::get('building/{id}/offices/edit', 'OfficeController@edit')->name('editofficeform');

Route::post('building/{id}/offices/edit', 'OfficeController@update')->name('editoffice');

Route::get('offices/{id}/delete', 'OfficeController@destroy')->name('deleteoffice');

Upvotes: 2

Views: 8861

Answers (4)

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

Use url()->previous():

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

If you want to return 2 requests back, use this solution.

Upvotes: 9

Nims Patel
Nims Patel

Reputation: 1056

In Laravel, you can do something like that:

<a href="{{Request::referrer()}}">Back</a> (assuming you're using blade).

{{ URL::previous() }}

Upvotes: 0

Rahul
Rahul

Reputation: 18557

You can try this function also,

/**
 * Method to redirect to the previous page
 *
 * Add to one of your helpers
 *
 * USEAGE: redirectPreviousPage();
 */
if ( ! function_exists('redirectPreviousPage'))
{
    function redirectPreviousPage()
    {
        if (isset($_SERVER['HTTP_REFERER']))
        {
            header('Location: '.$_SERVER['HTTP_REFERER']);
        }
        else
        {
            header('Location: http://'.$_SERVER['SERVER_NAME']);
        }

        exit;
    }
}

HTML

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

This function you also use it if there are core files in your project.

Upvotes: 0

Paladin
Paladin

Reputation: 1637

When using the "history.back" function, the browser used to give you a copy from it's cache instead of reload the page. Best way here is not to use the "history.back" function but use the correct route to get a fresh copy of your page.

Upvotes: 1

Related Questions