Search code examples
laravellaravel-3

Laravel3: Eloquent/Fluent fails during UPDATE statement


$account = Account::where('account_id', '=', $account_id)->first();
$account->username = 'New_Username';
$account->password = 'Password';
$account->save();

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause'

Why is this happening?


Solution

  • When you update an Eloquent model it will use the model's primary key. The default primary key is id, you can change this by adding the following in your class:

    public static $key = 'account_id';
    

    Be warned that there are some hard-coded references to id in Laravel, so the best advise is still to use id as a primary key when designing your databases for Eloquent.

    Reference: laravel/database/eloquent/model.php