Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 985
Chris Schmitz Avatar asked Mar 04 '16 20:03

Chris Schmitz


1 Answers

Nevermind, I found the answer after searching through the docs: https://laravel.com/docs/5.2/eloquent#eloquent-model-conventions

Under the section "Primary Keys" there's a little blurb:

In addition, Eloquent assumes that the primary key is an incrementing integer value. If you wish to use a non-incrementing primary key, you must set the $incrementing property on your model to false.

If you're going to use a UUID you have to set this property to false. Once I did that at the top of my model it worked.

Since all of my models are going to use UUIDs I extracted the UUID logic to a parent class. Here's what it looks like:

class UuidModel extends Model
{

    public $incrementing = false;

    /**
     * Sets the UUID value for the primary key field.
     */
    protected function setUUID()
    {
        $this->id = preg_replace('/\./', '', uniqid('bpm', true));
    }
}
like image 102
Chris Schmitz Avatar answered Oct 31 '22 05:10

Chris Schmitz