Reputation: 1762
My purpose :
1. get the id of the row from user input,
2. change the first row of selected row's values.
I have a model like this :
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();
}
}
Here's what happens :
1. I can post values that i get from inputs nicely
2. when it comes to the controller's save method, query updates every rows' values including that following ID number.
i.e. on the database
id=1; otherID=1; name=aaa; surname=bbb;
id=1; otherID=2; name=xxx; surname=yyy;
id=1; otherID=3; name=ccc; surname=ddd;
When i try to change name aaa->rrr that happens:
id=1; otherID=1; name=rrr; surname=bbb;
id=1; otherID=2; name=rrr; surname=yyy;
id=1; otherID=3; name=rrr; surname=ddd;
Thanks in advance.
Upvotes: 0
Views: 254
Reputation: 152860
Eloquent doesn't currently support composite primary keys. There's a discussion going on over at github if you want to show your support for the issue.
What you can do, is this:
myModel::where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->update([
'name' => Input::get('someNameInput'),
'surname' => Input::get('someSurnameInput')
]);
However, with this you use pretty much all model functionality. It's basically the same as:
DB::table('my_model')->where('id', Session::get('id'))
->where('otherID', Input::get('someHTMLInput'))
->update([
'name' => Input::get('someNameInput'),
'surname' => Input::get('someSurnameInput')
]);
Upvotes: 1
Reputation: 1379
If id
is the primary key, set it in database as primary, and autoincrement if needed. here in this code all ids are 1 so it updates all rows. You can set protected $primaryKey = 'id';
to protected $primaryKey = 'otherId';
, if you need to update data corresponding to otherId.
Upvotes: 0