HasUUID trait

This commit is contained in:
bad 2021-10-28 11:19:27 +02:00
parent ba7dbed00c
commit 74000681fc
3 changed files with 15 additions and 2 deletions

View file

@ -12,13 +12,24 @@ use Illuminate\Support\Str;
*/ */
trait HasUUID trait HasUUID
{ {
protected static function getPrimaryKey()
{
return self::uuidField();
}
/** /**
* Defines the UUID field for the model. * Defines the UUID field for the model.
* @return string * @return string
*/ */
protected static function uuidField() protected static function uuidField()
{ {
return 'id'; return 'uuid';
}
public function getIncrementing()
{
return false;
} }
/** /**

View file

@ -9,6 +9,8 @@ use Laravel\Sanctum\HasApiTokens;
class User extends Authenticatable { class User extends Authenticatable {
use HasApiTokens, HasFactory, HasUUID, Notifiable; use HasApiTokens, HasFactory, HasUUID, Notifiable;
public $primaryKey = "uuid";
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *

View file

@ -14,7 +14,7 @@ class CreateUsersTable extends Migration
public function up() public function up()
{ {
Schema::create('users', function (Blueprint $table) { Schema::create('users', function (Blueprint $table) {
$table->uuid("id")->unique()->primary(); $table->uuid("uuid")->unique()->primary();
$table->string('name'); $table->string('name');
$table->string('email')->unique(); $table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable(); $table->timestamp('email_verified_at')->nullable();