I have a mysql table in which I'm using a UUID as the primary key. Here's the creation migration:
Schema::create('people', function (Blueprint $table) {
$table->uuid('id');
$table->primary('id');
...
$table->timestamps();
}
Which generates the following MySQL schema:
CREATE TABLE `people` (
`id` char(36) COLLATE utf8_unicode_ci NOT NULL,
...
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
In my Eloquent model I have a method for creating an instance which calls a method for generating the UUID:
class Person extends Model
{
protected $fillable = [
...
];
public function make(array $personData){
$person = new Person;
$person->setUUID();
collect($personData)->each(function ($value, $columnName) use($person){
if(in_array($columnName, $this->fillable)){
$person->{$columnName} = $value;
}
});
$person->save();
return $person;
}
protected function setUUID(){
$this->id = preg_replace('/\./', '', uniqid('bpm', true));
}
}
When I create a new model instance it stores it fine in the database:
But when I try to access the new instance's id:
It returns as 0:
What am I missing here?


