Note that there are some explanatory texts on larger screens.

plurals
  1. POLaravel LastInsertId with UUID PK
    primarykey
    data
    text
    <p>I am attempting to use a UUID as a primary key using Laravel 4.</p> <p>Not finding much info about this topic I decided to use a trigger in MySQL to set the id column with a value of UUID() on insert.</p> <p>From what I have read, I need to set a var in the model of </p> <pre><code>public $incrementing = false; </code></pre> <p>In my migrations file I have something like this for each table:</p> <pre><code>//set default id to UUID, requires thread_stack in my.cnf to be 196k function makeTrigger($tableName) { DB::unprepared('CREATE TRIGGER '.$tableName.'_trigger_id BEFORE INSERT ON '.$tableName.' FOR EACH ROW SET NEW.id = UUID()'); } $tableName = 'sites'; Schema::create($tableName, function($table) { $table-&gt;string('id', 36)-&gt;primary(); $table-&gt;string('name', 50); $table-&gt;string('development_url', 50); $table-&gt;string('production_url', 50); $table-&gt;boolean('launched')-&gt;default(0); $table-&gt;string('test'); $table-&gt;timestamps(); $table-&gt;softDeletes(); }); makeTrigger($tableName); </code></pre> <p>While I can insert a record like this that has a UUID, I cannot return the ID if $incrementing = false is set in the model.</p> <p>If I remove that var and I am using a UUID, the id returned is 0. If I use increments('id') in the migrations file, I get the real id returned.</p> <p>I am building an app that UUIDs for ids in the spec, so I am trying to figure out if this is possible in Laravel 4.</p> <p>If I am unable to get that id back using </p> <pre><code>$user = User::create($userdata); return $user-&gt;id; </code></pre> <p>then how will the id be used for relationships? (Same issue using $user->save();)</p> <p>From what I understand Eloquent is expecting an auto_increment back, but it seems like there should be a way to get any id back.</p> <p>Why won't this work?</p> <p>Any insight into this area would be appreciated, as I can't seem to find any real documentation on this topic.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload