Eloquent find() fails with Unknown column 'id' in where clause
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `songs` where `id` = 5 limit 1)Short answer
Eloquent assumes the primary key is a column called id in the table named after the model. The error means one of those guesses is wrong. Compare the real table and its key to the model, then set protected $primaryKey or protected $table, or the owner key of a belongsTo relation.
Fix it fast
1Find out which cause you have
Look at the columns of the table named in the SQL, in the database itself, and read which one is the primary key.
php artisan db:table songs2Match what you see, then jump to its fix
- 1
db:table shows a key such as song_id and no id column.The primary key column is not called idFix - 2
The table in the SQL is not the one you meant, or it has no id because it is a pivot or legacy table.The model is bound to a different table than the one you expectFix - 3
The failing query is on the parent table and is triggered by loading a relation.A belongsTo relation looks up the parent by idFix
- 1
What does the error look like?
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select * from `songs` where `id` = 5 limit 1)
Cause 1 of 3
The primary key column is not called id
1Check
Eloquent assumes each table has a primary key column named id, and find() uses it.
grep -n "primaryKey" app/Models/Song.php2Fix
Tell the model the real key. If the key is not an auto-incrementing integer, also set $incrementing and $keyType.
class Song extends Model
{
protected $primaryKey = 'song_id';
}3Verify
find() now queries the right column.
php artisan tinker
>>> App\Models\Song::find(5)Confirmed by laravel.com
Cause 2 of 3
The model is bound to a different table than the one you expect
1Check
By convention the snake case, plural class name is the table name. A class named Song uses songs; AirTrafficController uses air_traffic_controllers.
grep -n "\$table" app/Models/Song.php2Fix
Declare the table name when it does not follow that rule.
protected $table = 'tbl_songs';3Verify
The SQL now names the right table.
php artisan tinker
>>> App\Models\Song::where('title', 'x')->toSql()Confirmed by laravel.com
Cause 3 of 3
A belongsTo relation looks up the parent by id
1Check
belongsTo defaults to matching the parent's primary key (id). When the parent uses a different column, the relation has to say so with its third argument.
grep -n "belongsTo" app/Models/*.php2Fix
Pass the foreign key and the parent's owner key.
public function album(): BelongsTo
{
return $this->belongsTo(Album::class, 'album_code', 'code');
}3Verify
The relation loads without the error.
php artisan tinker
>>> App\Models\Song::first()->albumConfirmed by laravel.com
How do I stop it coming back?
- Name primary keys id in new tables so the defaults work.
- For legacy tables, set $table and $primaryKey in the model before writing queries.
- Run php artisan db:table on an unfamiliar table before modelling it.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
- EZ EstimaterConstruction quoting and job tracking for contractors, on a Laravel and Firebase stack.
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
Related fixes
Back end
Laravel insert fails with 'Field user_id doesn't have a default value'
The insert works when the column is removed or made nullable
Error 1364 means Laravel sent an INSERT without a value for a NOT NULL column that has no default, and MySQL strict mode refuses it.
3 causes, each with a fixBack end
Laravel migration cannot drop a column that has a foreign key
Error on rename of '%s' to '%s' (errno: %d - %s)
MySQL refuses to drop a column while a foreign key constraint still uses it.
2 causes, each with a fixBack end
Laravel: distinct() and count() give the total instead of the unique count
select('city')->distinct()->count() ignores the select
Laravel builds the count from the column you pass to count(), not from select().
2 causes, each with a fixBack end
Laravel Eloquent query for a column that is X or NULL
where('column', null) returns no rows
Wrap the pair in a closure: where(function ($q) { $q->where('col', $x)->orWhereNull('col'); }).
3 causes, each with a fixBack end
Laravel insert fails with Invalid datetime format 1292 Incorrect datetime value
SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value
MySQL ran in strict mode and refused a date or time value that it cannot store.
3 causes, each with a fixBack end
php artisan migrate stops with could not find driver
could not find driver (Connection: mysql, SQL: select * from information_schema.tables ...)
Laravel talks to the database through a PDO driver, and this error means the PHP that runs artisan has none for your DB_CONNECTION.
3 causes, each with a fix