2016-02-08 72 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());