mercredi 10 octobre 2018

Laravel UUID's and their uniqueness?

I have two tables, one for lists and another that stores a history of lists that were created. These lists are very temporary and they can be deleted by numerous methods so I add a reason field on the history for that.

//Lists table
Schema::create('lists', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name');
  $table->text('message');
  $table->uuid('uuid');
  $table->timestamps();
});

//History table
Schema::create('history', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name');
  $table->text('message');
  $table->string('reason');
  $table->uuid('uuid');
  $table->timestamps();
});

Now both have a uuid field and I can generate an actual string to store with Laravel's helper function $uuid = (string) Str::uuid();

$list = new List;
$list->name = 'A basic fact of life';
$list->message = 'Pineapple does not belong on pizza.'
$uuid = (string) Str::uuid();
$list->uuid = $uuid;
$list->save();

Now when I successfully delete a record from Lists, I additionally create a new record in the history with its data.

$list = find($id);
$destroy = List::destroy($id);
if($destroy) {
  $history = new History;
  $history->name = $list->name;
  $history->message $list->message;
  $history->uuid = $list->uuid;
  $history->reason = 'some reason';
  $history->save();
}

So my question is, how will Laravel know that the next UUID I generate is actually unique?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire