因此,当我按下表格上的“编辑”按钮时,它应该显示给我编辑页面。但是无法显示该页面,editdataruang.blade.php中的Undefined variable:id_ruang出现了问题。我不知道怎么了请帮助我
这是编辑页面editruang.blade.php
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Whoops!</strong> There were some problems with your input.<br><br>
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<div class="x_content">
<form class="form-horizontal form-label-left" role="form" method="post" action="{{action('AdminController@updateruang', $id_ruang)}}">
{{csrf_field()}}
<input name="_method" type="hidden" value="PATCH">
<div class="form-group">
<input type="hidden" value="{{csrf_token()}}" name="_token" />
<label for="title">ID Ruang</label>
<input type="text" class="form-control" name="title" value={{$ruang->id_ruang}} />
</div>
</div>
这是AdminController
public function storeruang(Request $request)
{
$ruang = new Ruang();
$ruang->id_ruang = $request->id_ruang;
$ruang->nm_ruang = $request->nm_ruang;
$ruang->keterangan = $request->keterangan;
$ruang->save();
return redirect(url('/admin/dataruang'));
}
public function editruang(Ruang $id_ruang)
{
$ruang = Ruang::find($id_ruang);
return view('admin.editdataruang', compact('ruang', '$id_ruang'));
}
public function updateruang(Request $request, $id_ruang)
{
$ruang = new Ruang();
$data = $this->validate($request, [
'id_ruang'=>'required',
'nm_ruang'=>'required',
'keterangan'=>'required'
]);
$data['id_ruang'] = $id_ruang;
$ruang->updateTicket($data);
return redirect('/admin/dataruang')->with('success', 'New data has been updated!!');
}
这是路线
Route::get('/editdataruang{id_ruang}', 'AdminController@editruang', function () {
$users[] = Auth::user();
$users[] = Auth::guard()->user();
$users[] = Auth::guard('admin')->user();
return view('admin.editdataruang');
})->name('editdataruang');
Route::post('/editdataruang{id_ruang}','AdminController@updateruang', function(){
$users[] = Auth::user();
$users[] = Auth::guard()->user();
$users[] = Auth::guard('admin')->user();
})->name('editdataruang');
答案 0 :(得分:1)
您下面的editruang()
函数:
public function editruang(Ruang $id_ruang)
{
$ruang = Ruang::find($id_ruang);
return view('admin.editdataruang', compact('ruang', '$id_ruang'));
}
应写为:
public function editruang(Ruang $id_ruang)
{
$ruang = Ruang::find($id_ruang);
return view('admin.editdataruang', compact('ruang', 'id_ruang'));
}
答案 1 :(得分:0)
更改您的
<form class="form-horizontal form-label-left" role="form" method="post" action="{{action('AdminController@updateruang', $id_ruang)}}">
对于
<form class="form-horizontal form-label-left" role="form" method="post" action="{{action('AdminController@updateruang', $ruang->id_ruang)}}">
,您将无需在控制器id_ruang
中定义额外的变量