Reputation: 1
I am trying to update a record in my Laravel project using a validation rule, but I keep getting the following SQL error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (Connection: mysql, SQL: select count(*) as aggregate from classes where class_name = '10C' and id <> 1)
It seems Laravel is trying to use id
as the primary key in the WHERE
clause, but my primary key is class_id
.
Changing the validation rule to use class_id instead of id, but the error persists
Ensuring my model has protected $primaryKey = 'class_id'; Logging route('class') and route('class_id') to confirm it's retrieving the correct value.
Upvotes: 0
Views: 26
Reputation: 572
I am providing examples in my answer:
1. Check in the Model class that you have defined the primary key:
class YourModelClass extends Model
{
protected $primaryKey = 'class_id';
public $incrementing = false; // Only if your primary key is not auto incrementing
protected $keyType = 'string'; // Only if your primary key is not an integer
}
2. Ignore the ID by this type of validation rule:
$request->validate([
'class_name' => 'required|unique:classes,class_name,'.$class->class_id.',class_id',
]);
Upvotes: 0