所以我有一个办公室的搜索表单和办公室的删除,也编辑那些之前正在工作的那两个。但是在我更改并修复了编辑表单和按钮之后,那些搜索工作不再有效,显示此错误
Undefined variable: building (View: C:\xampp\htdocs\Eguide\resources\views\search.blade.php)
删除也会显示此错误,但是在我返回办公室页面后它会删除它但它显示错误消息:
Trying to get property 'name' of non-object (View: C:\xampp\htdocs\Eguide\resources\views\building.blade.php)
DD办公室结果
OfficeController.php
public function index()
{
$search = \Request::get('search');
$offices = Office::where('name','like','%'.$search.'%')->get();
return view('search',compact('offices','search'));
}
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()->route('building', $id);
}
public function show($id)
{
$office = Office::find($id);
return view('office')->withOffice($office);
}
public function edit($id, $office_id) {
$office = Office::find($office_id);
return view('editoffice', compact('office', 'id'));
}
public function update(Request $request, $id, $office_id)
{
// echo $id.'--'.$office_id;exit;
//$office = Office::find($id);
$office = Office::find($office_id);
$office->name = $request->officename;
$office->floor = $request->floor;
$office->update();
\Session::flash('building_flash', 'Updated successfully!');
return redirect()->route('building', $id);
}
public function destroy($id)
{
$office = Office::find($id);
$office->delete();
\Session::flash('building_flash_delete', 'Deleted successfully!');
return redirect()->route('building', $id);
}
}
search.blade.php
删除此内容时,搜索中没有错误
<a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$office->id])}}" class="btn btn-success btn-sm">Edit</a>
<div class="search">
{!! 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
</button>
</span>
</div>
{!! Form::close()!!}
</div>
<hr>
<table class="table">
<thead>
<th>Office Name</th>
<th>Belongs to</th>
<th>Office Floor</th>
</thead>
<tbody>
@foreach($offices as $office)
<tr>
<td>{{optional($office)->name}}</td>
<td>{{$office->building->name}}</td>
<td>{{$office->floor}}</td>
<td class="a">
@if(!Auth::guest())
<a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$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
@endforeach
@endsection
我在app文件夹中也有Building.php它有这样的代码
class Building extends Model
{
public $table = 'buildings';
public function offices(){
return $this->hasMany('App\Office');
}
}
Office.php
class Office extends Model
{
public function building(){
return $this->belongsTo('App\Building');
}
}
路线
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');
});
buildidng.blade.php
<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()!!}
<table class="table">
<div class="ttitle">
<thead>
<th>Office Name</th>
<th>Office Floor</th>
</thead>
<tbody>
@foreach($offices as $office)
<tr>
<td>{{optional($office)->name}}</td>
<td>{{$office->floor}}</td>
<td class="a">
@if(!Auth::guest())
<a href="{{route('editofficeform', ['id'=>$building->id, 'office_id'=>$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>
@endsection
答案 0 :(得分:0)
您的building.blade.php正在尝试访问$ building变量。控制器中的show方法仅将$ office作为数据传递。由于$ building不存在,试图获取它的name属性返回错误。
您需要将$ building从控制器传递到视图,或者它是$ office的一部分从该变量推断$ building。
public function show($id)
{
$office = Office::find($id);
$building = Buildng::where('id', '=', $office->building_id)->firstOrFail();
return view('office',compact('office', 'building'));
}
答案 1 :(得分:0)
building
中未定义show
。
public function show($id)
{
$office = Office::find($id);
return view('office')->withOffice($office);
}
您可以做的是在show方法中加载建筑物
public function show($id)
{
$office = Office::find($id);
$building = Building::first();//some logic to find building
return view('office',['building' => $building, 'office' => $office]);
}