我在基于 Laravel 的应用程序控制器 (SampleTypeController.php
) 中有以下 Edit 和 Update 方法。
public function edit(sampletype $type) {
return view('sampletypes.edit',compact('type'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\inventory $inventory
* @return \Illuminate\Http\Response
*/
public function update(Request $request, sampletype $type) {
try {
request()->validate([
'samp_name' => 'required',
'samp_desc' => 'required',
]);
$type->update($request->all());
return redirect()->route('sampletypes.index')
->with('success','Sample Type updated successfully');
} catch(Exception $e) {
return redirect()->route('sampletypes.index')
->with('failed','An error occured.');
}
}
我在 web.php
中有我提到的方法的路线,
Route::resource('sampletypes', SampleTypeController::class);
我已经在 web.php
的顶部声明了我的控制器
use App\Http\Controllers\SampleTypeController;
我可以通过索引刀片访问编辑刀片,这就是我的做法
<table class="table table-bordered">
<tr>
<th>No</th>
<th>Type Name</th>
<th width="280px">Action</th>
</tr>
@foreach ($sampletypes as $key => $sampletype)
<tr>
<td>{{ ++$i }}</td>
<td>{{ $sampletype->samp_name }}</td>
<td>
@can('role-edit')
<a class="btn btn-primary" href="{{ route('sampletypes.edit',$sampletype->id) }}">Edit</a>
@endcan
@can('role-delete')
{!! Form::open(['method' => 'DELETE','route' => ['sampletypes.destroy', $sampletype->id],'style'=>'display:inline']) !!}
{!! Form::submit('Delete', ['class' => 'btn btn-danger']) !!}
{!! Form::close() !!}
@endcan
</td>
</tr>
@endforeach
</table>
我在刀片中的编辑表单看起来像这样,
{!! Form::model($type, ['method' => 'PATCH','route' => ['sampletypes.update', $type->id]]) !!}
<div class="row">
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Type Name:</strong>
{!! Form::text('samp_name', null, array('placeholder' => 'Type Name','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12">
<div class="form-group">
<strong>Type Description:</strong>
{!! Form::text('samp_desc', null, array('placeholder' => 'Type Description','class' => 'form-control')) !!}
</div>
</div>
<div class="col-xs-12 col-sm-12 col-md-12 text-center">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
{!! Form::close() !!}
但是当我尝试访问我的编辑 URL (http://sample.test/sampletypes/1/edit
) 时,它不断给我以下错误
Missing required parameter for [Route: sampletypes.update] [URI: sampletypes/{sampletype}] [Missing parameter: sampletype]. (View: E:\MY_PROJECTS\jodulk\resources\views\sampletypes\edit.blade.php)
我正在努力寻找我做错了什么地方......
答案 0 :(得分:0)
尝试从控制器中 dd $sampletypes 以验证您是否正在获取 id。最佳做法是使用首字母大写作为模态,而不是 sampletype,您可以使用 Sampletype 或 SampleType。我个人更喜欢第二个,因为它更具可读性。
答案 1 :(得分:0)
Route::resource('sampletypes', SampleTypeController::class);
替换成这个
Route::resource('sampletypes', App\Http\Controllers\SampleTypeController::class);
试试这个我希望你能得到你的解决方案。