1

I'm having problems with the back button of my Createoffice form and Editoffice form it doesn't goes back to the page where the offices are listed

I tried using this:

  <a href="{{ route('building', $building->id) }}" class="btn btn-default">Back</a>

but nah its not working here's the error:

Undefined variable: building (View: C:\xampp\htdocs\Eguide\resources\views\createoffice.blade.php)

heres my routes:

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

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

  Route::get('offices', 'OfficeController@index');

  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');

This is how I make the data display

PageController.php

     public function buildings(){
            $buildings = Building::paginate(10);
            return view('buildings')->with('buildings', $buildings);
        }

        public function show($id){
            $building = Building::find($id);
            $offices = Office::where('building_id', $id)->orderBy('floor')->get();
            return view('building')->with('building', $building)->with('offices', $offices);

} }

OfficeController.php

class OfficeController extends Controller {

   /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {

        $search = \Request::get('search');

        $offices = Office::where('name','like','%'.$search.'%')->get();
        return view('search')->with('offices', $offices)->with('search', $search);

      }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    public function create($id)
    {

        return view('createoffice')->with('id', $id);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, $id)
    {
        $office = new Office();
        $office->name =$request->officename;
        $office->floor = $request->floor;
        $office->building_id = $id;
        $office->save();

        return redirect()->back();

         \Session::flash('building_flash', 'Created successfully!');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $office = Office::find($id);
        return view('office')->withOffice($office);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $office = Office::find($id);
        return view('editoffice')->withOffice($office)->with('id',$id);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $office = Office::find($id);
        $office->name =$request->officename;
        $office->floor = $request->floor;
        $office->update();

          \Session::flash('building_flash', 'Updated successfully!');
          return redirect()->back();

    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $office = Office::find($id);
        $office->delete();
\Session::flash('building_flash_delete', 'Deleted successfully!');
        return redirect()->back();

    }
TeamPlar Jarj
  • 121
  • 1
  • 13

4 Answers4

2

Name your route

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

and pass the variable as second argument to route

<a href="{{ route('building', ['id' => $id] ) }}" class="btn btn-default">Back</a>

Docs

linktoahref
  • 7,812
  • 3
  • 29
  • 51
1

Simply you can do that by Java Script

<button onclick="goBack()">Go Back</button>

<script>

function goBack() {
    window.history.back();
    }

</script>
Adam Kozlowski
  • 5,606
  • 2
  • 32
  • 51
1

You can hardcode the link:

<a href="{{ url('building/' . $id) }}" 

Or you can use the url()->previous() method:

<a href="{{ url()->previous() }}"
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
  • http://localhost:8000/building/1 this is the link that I want to the back button to go I have a lot of building with different id its like : http://localhost:8000/building/1 http://localhost:8000/building/2 – TeamPlar Jarj Jan 10 '18 at 11:56
  • Oh I've tried this but the problem of this one is when I click the create or update button it doesnt go back it stays on the page @Alexey Mezenin – TeamPlar Jarj Jan 10 '18 at 11:58
  • @TeamPlarJarj please show full controller method and full view. Or you can just [go 2 links back](https://stackoverflow.com/a/36098635/1227923) if you use a bunch of redirects. – Alexey Mezenin Jan 10 '18 at 12:01
  • Im having error in the Edit back button the create works but the edit it doesnt this is the error : Trying to get property 'name' of non-object (View: C:\xampp\htdocs\Eguide\resources\views\building.blade.php) @Alexey Mezenin – TeamPlar Jarj Jan 10 '18 at 12:17
  • @TeamPlarJarj it's not related to the question, it means now it's working and you're getting next error. To be able to help you with this one I need to see the full error message and the `building.blade.php`. I recommend you to ask a new question and add the view and the error message. – Alexey Mezenin Jan 10 '18 at 12:23
  • can we do this for routes usin post methods, if not is there a way to route back to a post route – RoshJ Oct 12 '20 at 05:45
0

Just call index route :

<a href="{{ route('offices') }}" class="btn btn-default">Back</a>
Maraboc
  • 10,550
  • 3
  • 37
  • 48
  • localhost:8000/building/1 this is the link that I want to the back button to go I have a lot of building with different id its like : localhost:8000/building/1 localhost:8000/building/2 @Maraboc – TeamPlar Jarj Jan 10 '18 at 11:59