首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >EditForm后退按钮和如何在Laravel中返回重定向

EditForm后退按钮和如何在Laravel中返回重定向
EN

Stack Overflow用户
提问于 2018-01-11 14:24:47
回答 1查看 929关注 0票数 0

我有这个编辑表单,但后退按钮不起作用,所以当我单击后退按钮时,它会显示这个错误,

这是我的createOffice的后退按钮,我试着将它应用于我的EditOffice后退按钮,但它不起作用:

代码语言:javascript
运行
复制
 <a href="{{ route('building', ['id' => $id] ) }}" class="btn btn-default">Back</a>

另外,如何使UpdateOffice按钮在单击时返回到我正在使用的return redirect()->back()的办公室列表页面,并且它停留在编辑表单页面中。正确的代码是什么?

以下是代码

OfficeController.php

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
    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

代码语言:javascript
运行
复制
  @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

路线

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
    @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
EN

回答 1

Stack Overflow用户

发布于 2018-01-11 15:12:14

我能看到的主要问题是您的路由定义和使用嵌套路由传递参数。例如,让我们以编辑办公路由为例;

代码语言:javascript
运行
复制
Route::get('building/{id}/offices/edit', 'OfficeController@edit')->name('editofficeform');

这实际上是一个嵌套路由,根据定义,它应该有两个参数;(1)您正在编辑的大楼的building_id,您已经指定为{id} (2)您还应该传递office_id,这将是您想要编辑的特定办公室的id。

正确的路由定义应该是这样的;

代码语言:javascript
运行
复制
Route::get('building/{id}/offices/{office_id}/edit', 'OfficeController@edit')->name('editofficeform');

然后在building.blade.php上,当您像这样指定编辑链接时;

代码语言:javascript
运行
复制
<a href="{{route('editofficeform', $office->id)}}" class="btn btn-success btn-sm">Edit</a>

应该改成这样;

代码语言:javascript
运行
复制
<a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>

在这里,您将传递主建筑id和特定的办公室id,您可以随心所欲地使用它们,并且可以跟踪您所在的建筑页面。

OfficeController.php的控制器端,你可以把你的edit()动作改成这样;

代码语言:javascript
运行
复制
public function edit($id, $office_id)
{
    $office = Office::find($office_id);
    return view('editoffice')->withOffice($office)->with('id',$id);
}

现在,您可以在office编辑表单上使用相同的后退按钮链接,它应该可以正常工作,并将您带回实际的构建页面。

代码语言:javascript
运行
复制
<a href="{{ route('building', ['id' => $id] ) }}" class="btn btn-default">Back</a>

我希望这个解释也能帮助解决其他类似的问题。

更新

因为您一直在使用此类型的其他路由时遇到问题。在editoffice.blade.php中,你可以像这样更新表单标签;

代码语言:javascript
运行
复制
{!! Form::open(array('route' => ['editoffice', ["id"=>$id, "office_id"=>$office->id]], 'class' => 'form')) !!}

并在OfficeController.php中更新您的update()方法以处理这个附加参数;

代码语言:javascript
运行
复制
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);
}

可以在createoffice.blade.php中使用相同的类比,在store()方法中也可以使用类似的类比。当然--不会有任何office_id

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48200942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档