Eloquent, Postgres, UUIDs and not getting id's back
Postgres has an excellent column type for using UUIDs as primary keys. The UUIDs are generated default by Postgres.
There is a small caveat working with UUIDs as primary keys and Laravel. Most tutorials go for this setup where you define $keytype = 'string
and $incrementing = false
in your model. But there is a problem with this:
class Site extends Model
{
protected $keyType = 'string'; // Pgsql's UUIDs are used instead
public $incrementing = false; // UUIDs are not incrementing, turn it off
// ...
}
$site = Site::create([...]);
$site->id; // null?
However, if we remove $incrementing
(which is true
by default), it’s working as expected.
class Site extends Model
{
protected $keyType = 'string';
// ...
}
$site = Site::create([...]);
$site->id; // 247c5acf-51ba-457f-8265-ef21b4cf4358