UUID primary key in Eloquent model is stored as uuid but returns as 0

Viewed 7354

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:

uuids stored

But when I try to access the new instance's id:

creating new instance and dumping id

It returns as 0:

returned result

What am I missing here?

1 Answers
Related