laravel day 5 section 3: edit and update

大背景知識,這里可以是認(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')) !!}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Laravel 學(xué)習(xí)交流 QQ 群:375462817 本文檔前言Laravel 文檔寫的很好,只是新手看起來會(huì)有...
    Leonzai閱讀 8,706評論 2 12
  • 一. 說明 以下內(nèi)容大部分引用Laravel China社區(qū)的文章 - 分享下團(tuán)隊(duì)的開發(fā)規(guī)范 ——《Laravel...
    knghlp508閱讀 8,011評論 0 28
  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對...
    cosWriter閱讀 11,631評論 1 32
  • 年輕人最迷茫的就是不知道自己這一生應(yīng)該做些什么。是的,我就是這些年輕人其中的一個(gè),我很迷茫,非常迷茫。 有些人很幸...
    深水里的星星閱讀 321評論 1 1
  • 這兩天都在準(zhǔn)備創(chuàng)業(yè)營及青年大會(huì)需要的服裝,許久沒來逛商場,有些店面都已經(jīng)不再是原來的樣子,經(jīng)過裝修后更加舒適、美觀...
    王金鳳_自我管理顧問閱讀 774評論 1 0

友情鏈接更多精彩內(nèi)容