dan*_*ade 10 model laravel eloquent
我有一张运动队的桌子.该记录显示了团队选择和其他一些信息.我想用团队选择更新记录.我的模型是这样的:
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'
];
Run Code Online (Sandbox Code Playgroud)
}
所以我有一个表单,它提供位置的所有数据,并给出DB中记录的id.在我的控制器中,我有:
public function storeFirstTeam()
{
$input = Request::all();
Selection::update($input->id,$input);
return redirect('first-team');
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
非静态方法Illuminate\Database\Eloquent\Model :: update()不应该静态调用,假设$ this来自不兼容的上下文
有谁可以指出我的愚蠢错误?
Jil*_*mas 26
请检查下面的代码,这将解决您的问题:
Selection::whereId($id)->update($request->all());
Run Code Online (Sandbox Code Playgroud)
你应该像下面给出的例子那样写:
Selection::where('id', $input['id'])->update($input);
// Or use this using dynamic where
Selection::whereId($input['id'])->update($input);
Run Code Online (Sandbox Code Playgroud)
或者,你也可以这样写:
Selection::find($input['id'])->fill($input)->save();
Run Code Online (Sandbox Code Playgroud)
该错误消息会告诉您您所知道的一切:您试图静态调用本来不是必须的方法(使用双冒号)。
该update()方法应在模型实例上调用,因此首先需要检索一个:
$selection = Selection::find($id);
Run Code Online (Sandbox Code Playgroud)
然后,您可以在该update()方法上:
$selection->update($request->all());
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
28508 次 |
| 最近记录: |