我的目的:
1.从用户输入获取行的ID,
2.更改所选行的值的第一行。
我有一个这样的模型:
myModel.php
class myModel extends Eloquent{
protected $table = 'myTable';
protected $primaryKey = 'id';
public $timestamps = false;
public incrementing = false;
protected fillable = array(
'id', //yes it needs to be fillable...
'name',
'surname'
);
}
myController.php
class myController extends BaseController{
public function someFunction(){
$modelInstance = myModel::where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->first();
$modelInstance->id = Session::get('id');
$modelInstance->name = Input::get('someNameInput');
$modelInstance->surname = Input::get('someSurnameInput');
$modelInstance->save();
}
}
这是发生的情况:
1.我可以很好地发布从输入中获取的值
2.当涉及到控制器的save方法时,查询会更新每行的值,包括后面的ID号。
即在数据库上
id=1; otherID=1; name=aaa; surname=bbb;
id=1; otherID=2; name=xxx; surname=yyy;
id=1; otherID=3; name=ccc; surname=ddd;
当我尝试更改名称aaa-> rrr时,会发生以下情况:
id=1; otherID=1; name=rrr; surname=bbb;
id=1; otherID=2; name=rrr; surname=yyy;
id=1; otherID=3; name=rrr; surname=ddd;
提前致谢。
雄辩的当前不支持复合主键。如果您想表达对此问题的支持,请在github上进行讨论。
您可以做的是:
myModel::where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->update([
'name' => Input::get('someNameInput'),
'surname' => Input::get('someSurnameInput')
]);
但是,您几乎可以使用所有模型功能。它基本上与以下内容相同:
DB::table('my_model')->where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->update([
'name' => Input::get('someNameInput'),
'surname' => Input::get('someSurnameInput')
]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句