2016-02-08 74 views
1

我有一张运动队的桌子。记录显示团队选择和其他一些信息。我想用团队选择更新记录。我的模型是这样的:使用Laravel模型的更新表格

class Selection extends Model { 

protected $table = "selection"; 

protected $fillable = [ 
    'loose', 
    'hooker', 
    'tight', 
    'secrow1', 
    'secrow2', 
    'blindflank', 
    'openflank', 
    'eight', 
    'scrum', 
    'fly', 
    'leftwing', 
    'rightwing', 
    'fullback', 
    'sub1', 
    'sub2', 
    'sub3', 
    'sub4', 
    'sub5' 
]; 

}

所以我有一个形式,给出了位置的所有数据,并给出了ID在数据库中记录。在我的控制,我有:

public function storeFirstTeam() 
{ 
    $input = Request::all(); 

    Selection::update($input->id,$input); 

    return redirect('first-team'); 
} 

,但我得到了以下错误:

Non-static method Illuminate\Database\Eloquent\Model::update() should not be called statically, assuming $this from incompatible context

任何人都可以指出我的傻错误?

+0

您必须先选择要更新的行。当您发布更新请求时,如何获得选择的“id”? –

+0

尝试像这样:'Selection :: whereId($ id) - > update($ request-> except(['_ method','_ token']));' –

回答

4

检查我的评论,这将解决问题:

Selection::whereId($id)->update($request->all()));

2

你应该写什么样子如下例子:

Selection::where('id', $input['id'])->update($input); 
// Or use this using dynamic where 
Selection::whereId($input['id'])->update($input); 

或者,您可以把它写这样还有:

Selection::find($input['id'])->fill($input)->save(); 
1

错误消息告诉你一切你知道:你是试图静态调用一个方法(使用双冒号),这并不意味着。

update()方法是指在一个模型被称为例如,所以首先您需要检索一个:

$selection = Selection::find($id); 

然后,您可以可以在update()方法上:

$selection->update($request->all());