0

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?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
Tiago
  • 4,233
  • 13
  • 46
  • 70

1 Answers1

3

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';
flyingL123
  • 7,686
  • 11
  • 66
  • 135