【发布时间】:2017-11-10 20:29:15
【问题描述】:
我设置了创建人员记录的路线,如下所示:
Route::get('bodymaker/create', 'BodyMakerController@create')->name('bodymaker.create');
Route::post('bodymaker', 'BodyMakerController@store')->name('bodymaker.store');
Route::get('bodymaker/{id}/edit', 'BodyMakerController@edit')->name('bodymaker.edit');
Route::put('bodymaker/{id}/update', 'BodyMakerController@update')->name('bodymaker.update');
Route::patch('bodymaker/{id}', 'BodyMakerController@update')->name('bodymaker.update');
生成新记录的控制器方法正常工作,如下所示:
public function create()
{
return View::make('bodymaker.create');
}
public function store(Request $request)
{
$person = new Person;
$person->first_name = $request->first_name;
$person->last_name = $request->last_name;
$person->save();
return View::make('person.store', ['person'=>$person]);
}
现在...我的问题是让编辑/更新表单正确绑定...
public function edit($id)
{
$person = Person::findOrFail($id);
return View::make('person.edit', ['person'=>$person]);
}
public function update($id)
{
// This route fails when I pass in the Request $request like in the store method.
//But how does the update method receive the form values differently than the store method(?)
$person = Person::findOrFail($id);
$person->first_name = $request->first_name;
$person->last_name = $request->last_name;
$person->save();
return View::make('person.update', ['person'=>$person]);
}
更新的表单如下所示
{{ Form::model($person, ['route' => ['person.edit', $person->id], 'method' => 'PUT'] }}
{{ csrf_field() }}
{!! Form::text('first_name'), Input::old('first_name') !!}
{!! Form::text('last_name'), Input::old('last_name') !!}
{!! Form::submit('Update Person') !!}
{!! Form::close() !!}
我似乎无法获取更新记录的表单,只能生成一条新记录。我不理解 Form::model 绑定的语法。当我将请求作为参数传递时,PUT 请求失败,所以我也不明白 Laravel 内部使用的 POST 和 PUT 之间的区别,尽管我不确定这是我的问题
这是我的工匠路线:列表
| web,auth |||GET|HEAD| bodymaker/{id}/edit | bodymaker.edit | App\Http\Controllers\BodyMakerController@edit
| web,auth |||PUT | bodymaker/{id}/update| bodymaker.update| App\Http\Controllers\BodyMakerController@update
【问题讨论】:
标签: php laravel-5 eloquent laravel-blade