使用Laravel模型更新表
我有一张运动队的桌子。记录显示团队选择和一些其他信息。我想用团队选择更新记录。因此,我的模型是:使用Laravel模型更新表,laravel,model,eloquent,Laravel,Model,Eloquent,我有一张运动队的桌子。记录显示团队选择和一些其他信息。我想用团队选择更新记录。因此,我的模型是: class Selection extends Model { protected $table = "selection"; protected $fillable = [ 'loose', 'hooker', 'tight', 'secrow1', 'secrow2', 'blindflank', 'openflank', 'e
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');
}
但我得到了以下错误:
假设$this来自不兼容的上下文,则不应静态调用非静态方法illumb\Database\elount\Model::update()
有人能指出我愚蠢的错误吗?你应该像下面给出的例子那样写:
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();
错误消息告诉您所知道的一切:您试图静态调用一个不应该调用的方法(使用双冒号)
update()
方法用于在模型实例上调用,因此首先需要检索一个:
$selection = Selection::find($id);
然后,您可以使用update()
方法:
$selection->update($request->all());
请检查以下代码,这将解决您的问题:
Selection::whereId($id)->update($request->all());
必须首先选择要更新的行。发布更新请求时,如何获取所选内容的
id
?请尝试以下操作:selection::whereId($id)->update($request->except([''方法',''令牌])
如果出现问题,此方法是否update()
getrollBack
?如果在DB::beginTransaction()
之间,请求可以包含一些自定义字段,例如,我将数组放在如下位置:Foydabaranda::whereId($request->id\u eslox)->update(['login'=>$request->login_-eslox,'parol'=>$request->parol_-eslox,'tuKiTu'=>$request->xeli_-tochka,];
也可以按照我的要求工作。