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?
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));
    }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With