我有这个编辑表单,后退按钮不起作用,所以当我点击后退按钮时显示此错误,
这是我的createOffice的后退按钮我尝试将它应用到我的EditOffice后台按钮并且它不起作用:
<a href="{{ route('building', ['id' => $id] ) }}" class="btn btn-default">Back</a>
另外,如何点击UpdateOffice按钮,返回到办公室列表的页面,我使用return redirect() - &gt; back();它保留在编辑表单页面中..是什么代码?
以下是代码
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
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
@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
路线
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
@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
答案 0 :(得分:0)
我能看到的主要问题是你的路线定义和带有嵌套路线的参数传递。例如,让我们采取编辑办公室路线;
Route::get('building/{id}/offices/edit', 'OfficeController@edit')->name('editofficeform');
这实际上是一个嵌套路由,根据定义应该期待两个参数; (1)您正在编辑的建筑物的building_id
,您指定为{id}
(2)您还应该传递office_id
,这将是该特定办公室的ID你想编辑。
正确的路线定义应该是这样的;
Route::get('building/{id}/offices/{office_id}/edit', 'OfficeController@edit')->name('editofficeform');
当你指定这样的编辑链接时,然后在building.blade.php
上;
<a href="{{route('editofficeform', $office->id)}}" class="btn btn-success btn-sm">Edit</a>
应该改为这个;
<a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>
在这里,您将传递主建筑物ID和您可以使用的特定办公室ID,并可以跟踪您所在的建筑物页面。
在OfficeController.php
的控制器端,您可以将edit()
操作更改为此;
public function edit($id, $office_id)
{
$office = Office::find($office_id);
return view('editoffice')->withOffice($office)->with('id',$id);
}
现在您可以在办公室编辑表单上使用相同的后退按钮链接,它应该可以正常工作,并且可以带您回到实际的建筑页面。
<a href="{{ route('building', ['id' => $id] ) }}" class="btn btn-default">Back</a>
我希望这种解释有助于解决此类任何其他问题。
由于您遇到此类其他路线的问题。在editoffice.blade.php
中,您可以像这样更新表单标记;
{!! Form::open(array('route' => ['editoffice', ["id"=>$id, "office_id"=>$office->id]], 'class' => 'form')) !!}
并在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);
}
您可以在createoffice.blade.php
中使用相同的类比,也可以在store()
方法中使用相同的类比。当然,不会有任何office_id
。