Tiago
Tiago

Reputation: 4470

Id naming convention in Laravel

In a recent project I had a custom id name for one of my tables, the user table (it was called user_id instead of simply id).

When I tried to insert data into it, I got the following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: update users set active = 1, code = , updated_at = 2015-01-20 21:28:14 where id is null)

I solved it by renaming my table's id column to id, thus suiting Laravel's preference.

However, if I absolutely had to use a custom id name on any given table while using Laravel, what would be the best solutions/workarounds to this problem?

Upvotes: 0

Views: 834

Answers (1)

flyingL123
flyingL123

Reputation: 8076

From the docs:

http://laravel.com/docs/4.2/eloquent

Note: Eloquent will also assume that each table has a primary key column named id. You may define a primaryKey property to override this convention. Likewise, you may define a connection property to override the name of the database connection that should be used when utilizing the model.

So, in your model, you would just add:

protected $primaryKey = 'user_id';

Upvotes: 3

Related Questions