大背景知識,這里可以是認(rèn)為的比較重要的部分。
laravel 不支持直接put來修改form
============================ 報(bào)錯(cuò)的原因與解決 ==========================
step 1 思路
我們先從數(shù)據(jù)庫里面把數(shù)據(jù)獲取,然后將數(shù)據(jù)放進(jìn)寫好的表格里面,在表格倆面修改好,最后提交
step 2:
寫一個(gè)放獲取數(shù)據(jù)的表格
{!! Form::open(array('action' => ['todolistController@update', $todo->id] , 'method' => 'POST')) !!}
{{ Form::bsText('text', $todo->text)}}
{{ Form::bsTextArea('body', $todo->body)}}
{{ Form::bsText('due', $todo->due)}}
{{ Form::hidden("_method", 'PUT')}}
{{ Form::bsSubmit('update', ['class'=>'btn btn-primary']) }}
{!! Form::close() !!}
這里需要注意,像我之前所說的,laravel是不支持put的,所以我們要一個(gè)hidden component老完成這個(gè)update
修改provider
public function boot()
{
Form::component('bsText', 'components.form.text', ['name', 'value' => null, 'attributes' => []]);
Form::component('bsTextArea', 'components.form.textarea', ['name', 'value' => null, 'attributes' => []]);
Form::component('bsSubmit', 'components.form.submit', ['value' => 'Submit', 'attributes' => []]);
Form::component('hidden', 'component.form.hidden', ['name', 'value' => null, 'attributes' => []]);
}
在component form里面寫一個(gè)專門提交的form
{{Form::hidden($name, $value, $attributes)}}
這樣就可以啦
step 2 獲取數(shù)據(jù)并且卸乳表格
這步應(yīng)該是由controller來完成的
public function edit($id)
{
$todo = todo::find($id);
return view('todos.edit')->with('todo', $todo);
}
step 3 根據(jù)修改的數(shù)據(jù)并且提交
public function update(Request $request, $id)
{
$todo = todo::find($id);
$todo->text = $request->input('text');
$todo->body = $request->input('body');
$todo->due = $request->input('due');
$todo->save();
return redirect('/')->with("success", 'update successfully');
}
這樣update的部分也就已經(jīng)完成了
============================ 報(bào)錯(cuò)的原因與解決 ==========================
報(bào)錯(cuò): Creating default object from empty value
原因:我把表格里面的obj也寫在了string里面
{!! Form::open(array('action' => ['todolistController@update', '$todo->id'] , 'method' => 'POST')) !!}
解決:
實(shí)際上應(yīng)該是這樣的
{!! Form::open(array('action' => ['todolistController@update', $todo->id] , 'method' => 'POST')) !!}