108

Can I change Eloquent model primary key.

I want to set primary key for example admin_id instead of 'id'?

I know I can change table name for model like

protected $table = "admin";

Is there something similar for primary key?

Vuk Stanković
  • 7,864
  • 10
  • 41
  • 65

6 Answers6

216

Yes

class User extends Eloquent {

    protected $primaryKey = 'admin_id';

}
phirschybar
  • 8,357
  • 12
  • 50
  • 66
  • 17
    For anyone else referencing this question, use one of the other answers. `$primarykey` should be `$primaryKey` (uppercase letter K) and won't work otherwise. – Jeremy Harris Feb 07 '14 at 20:07
  • 4
    What if I have more than one fields as primary key? – Bagusflyer May 17 '14 at 04:49
  • @bagusflyer You are kidding right ? More than one primary key in one database table? – Novica89 Oct 01 '14 at 10:39
  • @bagusflyer Eloquent doesn't support composite keys, but here's a good workaround so you can have `$primaryKey = array('key1', 'key1');` https://github.com/laravel/framework/issues/5517#issuecomment-52996610 – mopo922 Jan 27 '15 at 06:36
  • Just FYI it looks like in Laravel 5.4 you no longer need set your own PK if it's the same as the model name as it adds it for you (and will error out otherwise). – Mint Feb 07 '17 at 21:08
24

If you are wanting to use a composite key (a string)

You need to make sure you set public $incrementing = false otherwise laravel will cast the field to an Integer, giving 0

class User extends Model {

    protected $primaryKey = 'my_string_key';
    public $incrementing = false;

}
user8555937
  • 2,161
  • 1
  • 14
  • 39
Toby Mellor
  • 8,093
  • 8
  • 34
  • 58
  • I did an override on the primaryKey as a text string col - the value in the collection was correct but when I referenced the col as $results->primary_key it would only return zero although the collection/array showed the correct string - took awhile to track it down when I noticed that the primaryKey was showing as an INT and Incrementable in the Object. Had to set public $incrementing = false. – G-Man Feb 26 '20 at 22:46
  • 1
    This comment really helped me. $v->SYS_ID was changing into a different number after save(). I COULD NOT figure out why. Thanks! – PhillipMcCubbin Aug 01 '22 at 19:27
15
class User extends Eloquent {

    protected $primarykey = 'admin_id';

}

but

class User extends Eloquent {

    protected $primaryKey = 'admin_id';

}

note the letter K (capital) on the variable $primaryKey

user3186817
  • 159
  • 2
7

The primary key variable is case sensitive and must be $primaryKey to work.

Example:

protected $primaryKey = 'your_primary_key_id';

Example within a Model class:

class User extends Eloquent {

    protected $primaryKey = 'your_primary_key_id';

}
ajtrichards
  • 29,723
  • 13
  • 94
  • 101
2
class User extends Eloquent {

    protected $primaryKey = 'admin_id';

}

As per Laravel documentation :


Eloquent will also assume that each table has a primary key column named id. You may define a $primaryKey property to override this convention.

In addition, Eloquent assumes that the primary key is an incrementing integer value, which means that by default the primary key will be cast to an int automatically. If you wish to use a non-incrementing or a non-numeric primary key you must set the public $incrementing property on your model to false.

Gammer
  • 5,453
  • 20
  • 78
  • 121
-1

To assign a primary key you should:

class User extends Eloquent {

    protected $primaryKey = 'admin_id';

}
Mahmoud Abdelsattar
  • 1,299
  • 1
  • 15
  • 31
Jay Momaya
  • 1,831
  • 19
  • 32