dreamer1231 2015-01-31 18:14
浏览 15
已采纳

使用Laravel更新数据库

I am trying to update information in my database with Laravel. Not sure what I am doing wrong but I can't seem to find where the problem is. Here is the code for my edit page (This is the page where I would edit information taken from my DB).

{{ Form::open(['url'=>'portfolio/update']) }}
    <div>
        {{ Form::label('portfolio_title', 'Portfolio Title:') }}
        {{ Form::text('portfolio_title',$work->portfolio_title) }}
        {{ $errors->first('portfolio_title','<span class="error">:message</span>') }}
    </div>
    <div>
        {{ Form::label('portfolio_description', 'Portfolio Description') }}<br>
        {{ Form::textarea('portfolio_description', $work->portfolio_description, ['size' => '50x5']) }}
        {{ $errors->first('portfolio_description','<span class="error">:message</span>') }}
    </div>
    <div>
        {{ Form::label('portfolio_content', 'Portfolio Content') }}<br>
        {{ Form::textarea('portfolio_content', $work->portfolio_content, ['size' => '50x5']) }}
        {{ $errors->first('portfolio_content','<span class="error">:message</span>') }}
    </div>
    {{ Form::hidden('id',$work->id) }}
    <div>
        {{ Form::submit('Update Work') }}
    </div>
{{ Form::close() }}

I have a controller called PortfolioController that will save info to database and what not.

public function edit($work_title){
    $work = Portfolio::wherePortfolio_title($work_title)->first();
    return View::make('portfolio/edit', ['work' => $work]);
}

public function update(){
    $id = Input::get('id');
    $input = Input::except('id');

    if( !$this->portfolio->fill($input)->isValid()){
        return Redirect::back()->withInput()->withErrors($this->portfolio->errors);
    }

    $work = Portfolio::find($id);

    $work->portfolio_title = Input::get('id');

    $work->save();

}

Here is my route that I am working with:

Route::resource('portfolio','PortfolioController');
Route::post('portfolio/update','PortfolioController@update');

I am able to get the form populated with the correct information but when i change something like the title and click update, the page reloads but does not save in the DB. Sometimes I will get an MethodNotAllowedHttpException error. This has been pretty frustrating for me so any help will be greatly appreciated.

  • 写回答

1条回答 默认 最新

  • douzhulv1699 2015-01-31 18:32
    关注

    Why don't you just actually use your resource route?

    First, remove the portfolio/update route. You don't need it.

    Then change your Form::open to this:

    {{ Form::open(['route' => ['portfolio.update', $work->portfolio_title], 'method' => 'put']) }}
    

    This way you target the update method in your RESTful controller.

    Finally change that to use the portfolio_title as identifier and you should be able to remove the hidden id field from your form.

    public function update($work_title){}
    

    Please take a look at:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c