Laravel Model saving data without primary key
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
In your given scenario, you have two models - Profile and Student. The profile model follows the traditional pattern with a primary key named 'id'. However, the student model does not have any primary key but rather a foreign key for linking to the profile model via the 'profile_id' column. This is an example of implementing relationships between these models. When you want to update the status of a student in your StudentController::activate() function, you're trying to use your Eloquent models' save() method. The issue here is related to saving data without specifying a primary key. As you have set $primary_key and $incrementing properties to null for your Student model, Laravel is unsure how to deal with its unique identifier when saving or updating data. In this case, it tries to use the id column which doesn't exist in your Student table, leading to the error message "Column not found: 1054 Unknown column 'id' in 'where clause.'" A better approach for your situation would be to explicitly define the primary key for your Student model. You could set up a custom primary key name or even use auto-incrementing values, depending on your needs. For example, you can add the following line in your Student model:
public $primaryKey = 'student_id';class Student extends Eloquent {
// ... existing properties and methods
public function profile() {
return $this->belongsTo('App\Profile', 'profile_id');
}
}
class Profile extends Eloquent {
// ... existing properties and methods
public function student() {
return $this->hasOne('App\Student', 'student_id');
}
}