Reputation: 2239
I have 2 models: Profile and Student. The profile contains a primary key, id. Student model doesn't have any primary key, but one foreign key, profile_id, which links to profile.id. I have a function within my Student controller that activates the Student, setting the status from 0 to 1, when called. I am trying to use save() for the update of the value, but unable to do it, prompting me a message as such:
Column not found: 1054 Unknown column 'id' in 'where clause'
Can please advise? Thank you very much.
Student Model:
class Student extends Eloquent {
protected $primary_key = null;
public $incrementing = false;
protected $fillable = array('username', 'password', 'status', 'profile_id');
public function profile() {
return $this->belongsTo('Profile');
}
}
Profile Model:
class Profile extends Eloquent {
protected $fillable = array('firstname', 'lastname', 'email', 'phone');
public function student()
{
return $this->hasOne('Student', 'profile_id', 'id');
}
}
StudentController:
public function activate($id) {
$student = Student::where('profile_id', '=', $id)->first();
$student->status = 1;
$student->save();
return Redirect::to('students');
}
Upvotes: 35
Views: 66655
Reputation: 144
Although this question is somewhat old, I think I can contribute something.
Using models without a primary key is not something to be promoted: WHENEVER POSSIBLE YOU SHOULD ADD A PRIMARY KEY.
Having said the above, I will explain how to handle these situations where the model does not have a primary key, either due to a design flaw or because the model itself does not represent an entity but a relationship between two or more entities. For example, the pivot tables.
The correct way is to override the setKeysForSaveQuery method of the Model class, in the model that has no primary key. This allows you to modify the id = $id condition that Laravel tries to do by default.
Let's see:
class Student extends Eloquent
{
protected $fillable = ['username', 'password', 'status', 'profile_id'];
public function profile()
{
return $this->belongsTo('Profile');
}
/**
* Set the keys for a select query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
*
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function setKeysForSelectQuery($query)
{
$query->where('profile_id', '=', $this->profile_id);
}
}
I must clarify that it is not really a trick, Laravel actually does it in the case of pivot tables.
Obviously I give this answer for cases where there really is no primary key or the update condition involves a combination of columns. In this question it could well be assumed that profile_id is the primary key since the relationship student:profile is one to one, let's see:
class Student extends Eloquent
{
protected $primaryKey = 'profile_id';
public $incrementing = false;
protected $fillable = ['username', 'password', 'status', 'profile_id'];
public function profile()
{
return $this->belongsTo('Profile');
}
}
I hope this helps.
Upvotes: 1
Reputation: 1135
If anyone has the same issue, I used Nishchit Dhanani answer and it worked pretty well:
Write this line into your Model:
public $primaryKey = '_id';
Where _id
is your manual primary key.
For my case I had a user table and part of my users are students. So the primary key in my students table was "user_id" and I added this line in my Student
model:
public $primaryKey = 'user_id';
I was able to use the save()
method when updating my student object.
In your case you would just need to add this to your student model:
public $primaryKey = 'profile_id';
Upvotes: -1
Reputation: 1035
I had the same problem and couldn't use a primary key field for this table.
Since Eloquent cannot update tables like this, I solved it simply deciding updating the row in a different way:
NameOfMyTable::where('entity_type', 1)->update(['last_import' => Carbon::now()]);
Upvotes: 6
Reputation: 3026
You might want to consider adding an id
field. It is pretty important to Laravel and packages (for aggregation, specifically)
but if you must...
Student Model: (camel case primaryKey
property)
class Student extends Eloquent {
protected $primaryKey = null;
public $incrementing = false;
protected $fillable = array('username', 'password', 'status', 'profile_id');
public function profile() {
return $this->belongsTo('Profile');
}
}
Upvotes: 68