如何调用此路线作为后退按钮,Laravel

时间:2018-01-10 11:47:58

标签: php laravel

我的Createoffice表单和Editoffice表单的后退按钮出现问题,它不会返回列出办公室的页面

我试过用这个:

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

但是它不在这里工作是错误:

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

继承我的路线:

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

这是我进行数据显示的方式

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

类OfficeController扩展了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();

    }

4 个答案:

答案 0 :(得分:2)

为您的路线命名

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

并将变量作为第二个参数传递给route

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

Docs

答案 1 :(得分:1)

您可以对链接进行硬编码:

export class Loader1 implements Loader2 {
    constructor(private http: HttpClient) {
    }

    public isObject(item) {
        return (item && typeof item === 'object' && !Array.isArray(item));
    }

    public mergeDeep(target, ...sources) {
        if (!sources.length) { return target; }
        const source = sources.shift();
        if (this.isObject(target) && this.isObject(source)) {
            for (const key in source) {
                if (this.isObject(source[key])) {
                    if (!target[key]) {
                        Object.assign(target, { [key]: {} });
                    }
                    this.mergeDeep(target[key], source[key]);
                } else {
                    Object.assign(target, { [key]: source[key] });
                }
            }
            return this.mergeDeep(target, ...sources);
        }
    }

    public getMethod(lang: string): any {
        return Observable.forkJoin(
            this.http.get(path1 + '.json').map((res) => res)
                .catch((res) => Observable.of(null)),
            this.http.get(path2 + '.json').map((res) => res),
            this.http.get(path3 + '.json').map((res) => res)
        ).map(results => {

            return this.mergeDeep({}, results[2], results[1], results[0]);

        });
    }
}

或者您可以使用url()->previous()方法:

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

答案 2 :(得分:0)

只需通过Java Script

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

<script>

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

</script>

答案 3 :(得分:0)

只需拨打索引路线:

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