简体   繁体   English

EditForm后退按钮以及如何在Laravel中返回重定向

[英]EditForm back button and how to return redirect in Laravel

I have this edit form and the back button is not working, So when I clicked the back button it shows this error, 我有此编辑表单,并且后退按钮不起作用,所以当我单击后退按钮时,它将显示此错误,

This is the backbutton for my createOffice I tried applying it to my EditOffice backbutton and it didnt work: 这是我的createOffice的后退按钮,我尝试将其应用到EditOffice后退按钮,但没有成功:

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

Also, how do I make the UpdateOffice button when clicked it goes back to the page for the list of offices I'm using return redirect()->back(); 另外,如何在单击UpdateOffice按钮时返回到我正在使用的办公室列表页面的页面return return()-> back(); and it stays in the edit form page.. whats the right code for this? 并保留在编辑表单页面中。正确的代码是什么?

Here are the codes 这是代码

OfficeController.php OfficeController.php

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

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

    return redirect()->back();


}

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

    }
}

PageController.php PageController.php

    class PageController extends Controller
    {
        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);


    } }

Building.blade.php Building.blade.php

  @extends('layouts.main')
  @section('title',$building->name)
  @section('css')
  @stop
  @section('content')

  <div class="officebg">
  <link href="https://fonts.googleapis.com/css?family=Anton" rel="stylesheet">

<div class="Bldgttl">

  <div class="container">
    <div class="row">
      <div class="col-lg-12">
          <img src="{{URL::to('/assets')}}/{{$building->picture}}" alt="" style="height:300px; width:500px;">
      </div>
    </div>
    <div class="row">
        <div class="col-lg-12">
          {{$building->name}}
        </div>

    </div>
  </div>
</div>

    <div class="rows">

    <div class="col-md-6 col-md-offset-3">

      <div class="col-xs-4 col-md-6">
 @if(!Auth::guest())
                <a href="{{route('createofficeform', $building->id)}}" class="btn btn-primary btn-md">Create an Office</a>
  @endif
        </div>


      {!! Form::open(['method'=> 'GET','url'=>'offices','role'=>'search']) !!}
    <div class="input-group col-xs-4 col-md-6" >
      <input type="text" name="search" class="form-control" placeholder="Search...">
     <span class="input-group-btn">
       <button type="submit" class="btn btn-info btn-md">Search</i>
       </button>
      </span>
        </div>
         {!! Form::close()!!}
          <hr>
    <table class="table">

      <thead>
          <th>Office Name</th>
          <th>Office Floor</th>
      </thead>

      <tbody>
        @foreach($offices as $office)
          <tr>

            <td>{{$office->name}}</td>
            <td>{{$office->floor}}</td>
            <td class="a">
 @if(!Auth::guest())
              <a href="{{route('editofficeform', $office->id)}}" class="btn btn-success btn-sm">Edit</a>
              <a href="{{route('deleteoffice', $office->id)}}" class="btn btn-danger btn-sm">Delete</a>
              @endif
            </td>
          </tr>
        @endforeach
      </tbody>
    </table>
  </div>
  </div>
</div>

    @endsection

Routes 路线

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

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

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

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

Route::group(['middleware' => ['auth']], function () {

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

  Route::post('building/create/store', 'BuildingController@saveBuilding')->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('building/{id}/offices/create', 'OfficeController@create')->name('createofficeform');

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

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

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

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

editoffice.blade.php editoffice.blade.php

    @extends('layouts.main')
@section('title', 'Create an Office')
@section('content')

{!! Form::open(array('route' => ['editoffice', $id], 'class' => 'form')) !!}
<div class="container">

            <div class="form-group">
                {!! Form::label('Office Name') !!}
                {!! Form::text('officename', $office->name,        array('required',
                          'class'=>'form-control',
                          'placeholder'=>'Office Name')) !!}
            </div>
            <div class="form-group">
                {!! Form::label('Office Floor') !!}
                {!! Form::text('floor', $office->floor,        array('required',
                          'class'=>'form-control',
                          'placeholder'=>'Office Floor')) !!}
            </div>

<div class="form-group">
    {!! Form::submit('Update Office',
      array('class'=>'btn btn-primary')) !!}


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



</div>
{!! Form::close() !!}

@endsection

The main problem that I can see, is with your routes definitions and passing of parameters with nested routes. 我能看到的主要问题是您的路由定义以及嵌套路由的参数传递。 For example lets take edit office route; 例如让我们编辑办公室路线;

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

This is actually a nested route which by definition should be expecting two parameters; 实际上,这是一条嵌套的路由,根据定义,该路由应包含两个参数: (1) The building_id of the building you are editing, that you have specified as {id} (2) you should also pass the office_id , which will be the id of that specific office which you want to edit. (1)您正在编辑的building_id的building_id,已指定为{id} (2),还应传递office_id ,这将是您要编辑的特定办公室的ID。

The correct route definition should be something like this; 正确的路由定义应该是这样的;

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

And then on building.blade.php when you are specifying edit link like this; 然后,当您指定这样的编辑链接时,在building.blade.php

<a href="{{route('editofficeform', $office->id)}}" class="btn btn-success btn-sm">Edit</a>

It should be changed to this; 应该更改为此;

<a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>

Here you will be passing the main building id and that specific office id which you can use however you want and can keep track of which building page you are on. 在这里,您将传递主要建筑物ID和您可以使用的特定办公室ID,并可以跟踪正在使用的建筑物页面。

On controller side in OfficeController.php you can change your edit() action to this; OfficeController.php控制器端,您可以将edit()动作更改为此;

public function edit($id, $office_id)
{
    $office = Office::find($office_id);
    return view('editoffice')->withOffice($office)->with('id',$id);
}

Now you can use your same back button link on office edit form and it should work fine and should take you back to actual building page. 现在,您可以在Office编辑表单上使用相同的后退按钮链接,它应该可以正常工作,并应带您回到实际的建筑页面。

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

I hope this explanation helps to solve the any other issues of this kind as well. 我希望这种解释也有助于解决其他任何此类问题。

Update 更新资料

As you have been having issue with other routes of this type. 由于您对这种其他路线有疑问。 In editoffice.blade.php , you can update your form tag like this; editoffice.blade.php ,您可以像这样更新表单标签;

{!! Form::open(array('route' => ['editoffice', ["id"=>$id, "office_id"=>$office->id]], 'class' => 'form')) !!}

And in OfficeController.php update your update() method to handle this additional parameter; 然后在OfficeController.php更新您的update()方法来处理此附加参数;

public function update(Request $request, $id, $office_id)
{
    $office = Office::find($office_id);
    $office->name =$request->officename;
    $office->floor = $request->floor;
    $office->update();

      \Session::flash('building_flash', 'Updated successfully!');
    //return redirect()->back();
    // You can replace this statement with the following to redirect the user to building page, rather than going back to edit form.
    return redirect()->route('building', $id);
}

You can use same analogy in createoffice.blade.php and similarly in store() method. 您可以在createoffice.blade.php使用相同的类比,并在store()方法中使用类似的类比。 Of-course there won't be any office_id . 当然不会有任何office_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM