Laravel基础知识:
我有以下路线:
Route::group(['prefix' => 'pps', 'as' => 'pps.', 'middleware' => ['auth']], function (){
Route::get('/index', 'PPS\PPSController@index')->name('index');
/**
* Templates
*/
Route::group(['prefix' => 'templates', 'as' => 'templates.', 'middleware' => ['auth']], function (){
Route::get('/', 'PPS\Template\TemplateController@index')->name('index');
/**
* Sequence group
*/
Route::group(['prefix' => 'sequenceGroup', 'as' => 'sequenceGroup.', 'middleware' => ['auth']], function (){
Route::get('/', 'PPS\Template\SequenceGroupController@index')->name('index');
Route::get('/create', 'PPS\Template\SequenceGroupController@create')->name('create');
Route::post('/store', 'PPS\Template\SequenceGroupController@store')->name('store');
Route::get('/edit/{sequenceGroup}', 'PPS\Template\SequenceGroupController@edit')->name('edit');
Route::put('/update/{sequenceGroup}', 'PPS\Template\SequenceGroupController@update')->name('update');
Route::delete('/delete/{sequenceGroup}', 'PPS\Template\SequenceGroupController@delete')->name('delete');
});
/**
* Sequence template
*/
Route::group(['prefix' => 'sequenceTemplates', 'as' => 'sequenceTemplates.', 'middleware' => ['auth']], function (){
Route::get('/{sequenceGroup}', 'PPS\Template\SequenceTemplateController@index')->name('index');
Route::get('/create/{sequenceGroup}', 'PPS\Template\SequenceTemplateController@create')->name('create');
Route::post('/store', 'PPS\Template\SequenceTemplateController@store')->name('store');
Route::get('/edit/{sequenceTemplate}', 'PPS\Template\SequenceTemplateController@edit')->name('edit');
Route::put('/update/{sequenceTemplate}', 'PPS\Template\SequenceTemplateController@update')->name('update');
Route::delete('/delete/{sequenceTemplate}', 'PPS\Template\SequenceTemplateController@delete')->name('delete');
});
});
});
当我更新序列组时,一切都运行良好。 但是当我将更新序列模板时,laravel总是编辑方法而不是更新方法。
这是我的表格:
<form action="{{ route('pps.templates.sequenceTemplates.update', $sequenceTemplate->id) }}" method="post">
{{ csrf_field() }}
{{ method_field('put') }}
<div class="form-group{{ $errors->has('name') ? ' has-error' : '' }}">
<label for="name" class="control-label">@lang('pps.name')</label>
<input type="text" name="name" id="name" class="form-control" value="{{ old('name', $sequenceTemplate->name) }}">
@if ($errors->has('name'))
<span class="help-block">
<strong>{{ $errors->first('name') }}</strong>
</span>
@endif
</div>
<div class="form-group{{ $errors->has('description') ? ' has-error' : '' }}">
<label for="description" class="control-label">@lang('pps.description')</label>
<input type="text" name="description" id="description" class="form-control" value="{{ old('description', $sequenceTemplate->description) }}">
@if ($errors->has('description'))
<span class="help-block">
<strong>{{ $errors->first('description') }}</strong>
</span>
@endif
</div>
<button type="submit" class="btn btn-primary">@lang('pps.save')</button>
</form>
控制器:
public function edit(SequenceTemplate $sequenceTemplate)
{
return view('pps.template.sequenceTemplate.edit', compact('sequenceTemplate'));
}
public function update(UpdateSequenceTemplateRequest $request, SequenceTemplate $sequenceTemplate)
{
$sequenceTemplate->update($request->except('_token', '_method'));
return redirect()->route('pps.templates.sequenceTemplate.index')->withSuccess(__('sequenceTemplateUpdated'));
}
请求:
<?php
namespace App\Http\Requests\PPS\Template;
use Illuminate\Foundation\Http\FormRequest;
class UpdateSequenceTemplateRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'sequence_group_id' => 'required|integer',
'name' => 'required|string|min:3',
];
}
}
有什么问题?我找不到这个错误。
答案 0 :(得分:1)
当您填写表单并按提交按钮时,Laravel会验证数据并重定向您,因为表单中没有sequence_group_id
并且该字段是必需的:
'sequence_group_id' => 'required|integer',
并且您没有看到任何错误消息,因为您没有尝试将其显示为sequence_group_id
。为了测试它,将它放在表单的顶部:
Errors: {{ dump($errors->all()) }}
并尝试提交表单。