rap2hpoutre/has-uuid

同时使用自增和UUID

v0.0.6 2018-02-19 12:38 UTC

This package is auto-updated.

Last update: 2024-09-07 00:17:53 UTC


README

是什么?

当你可以使用 两者 时,为什么要在序列ID(即自增)和UUID之间做出选择?此包可以帮助你将UUID添加到Laravel模型中。

如果你 想使用UUID而不需要自增,你也可以使用这个类。

快速入门

使用composer安装

composer require rap2hpoutre/has-uuid

将特质添加到你的模型中

<?php
class User {
    
    use \Rap2hpoutre\HasUuid\HasUuid;
    
    // ...
}

使用特质来加载你的模型

$user = User::uuid('e3ae1e6b-fabb-4839-bf65-de9a892c0d56');

并且当您保存模型时,它将 神奇地 为它添加一个UUID

$user = new User;
$user->name = 'raph';
$user->save(); // <- Your user has now a UUID (and a ID if you have not removed it)

PS:别忘了在你的迁移中添加UUID

<?php   
   class CreateSesNotificationsTable extends \Illuminate\Database\Migrations\Migration
   {
       public function up()
       {
           \Schema::create('user', function ($table) {
               $table->increments('id');
               $table->uuid('uuid')->index(); // <- THIS.
               $table->string('email')->index();
               $table->timestamps();
           });
       }
       // ...
   }