TeamPlar Jarj
TeamPlar Jarj

Reputation: 121

How to call this route for back button , Laravel

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

    }

Upvotes: 1

Views: 2166

Answers (4)

Alexey Mezenin
Alexey Mezenin

Reputation: 163758

You can hardcode the link:

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

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

<a href="{{ url()->previous() }}"

Upvotes: 1

linktoahref
linktoahref

Reputation: 7972

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

Upvotes: 2

Maraboc
Maraboc

Reputation: 11083

Just call index route :

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

Upvotes: 0

Adam Kozlowski
Adam Kozlowski

Reputation: 5896

Simply you can do that by Java Script

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

<script>

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

</script>

Upvotes: 1

Related Questions