2

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');
Rahul
  • 18,271
  • 7
  • 41
  • 60
TeamPlar Jarj
  • 121
  • 1
  • 13

4 Answers4

9

Use url()->previous():

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

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

Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • It wont go back to my officepage, when I click the create office and click back button it stays on the createofficepage @Alexey Mezenin – TeamPlar Jarj Jan 02 '18 at 09:26
  • @TeamPlarJarj do you use `GET` request when you're sending the form? You should use `POST`. Or just return [2 requests back](https://stackoverflow.com/questions/36098589/how-to-return-back-twice-in-laravel). – Alexey Mezenin Jan 02 '18 at 09:29
  • @TeamPlarJarj the right way is to use `POST` when submitting a form to `store()` method. https://laravel.com/docs/5.5/controllers#resource-controllers – Alexey Mezenin Jan 02 '18 at 09:32
  • Oh wait its a post .. I use it for the createoffice – TeamPlar Jarj Jan 02 '18 at 09:34
  • @TeamPlarJarj are you sure it's `POST`? Because you're using `GET` in all forms you've shown in previous questions. Also, have you tried 2 links back solution? – Alexey Mezenin Jan 02 '18 at 09:42
1

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.

Paladin
  • 1,637
  • 13
  • 28
0

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.

Rahul
  • 18,271
  • 7
  • 41
  • 60
0

In Laravel, you can do something like that:

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

{{ URL::previous() }}

Nims Patel
  • 1,048
  • 9
  • 19
  • Ya but the problem is when I press Create Office button and click the back button it doesnt go back to officepage it stays on createofficepage – TeamPlar Jarj Jan 02 '18 at 09:28