35 lines
612 B
PHP
35 lines
612 B
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
/*
|
|
* A trait for all models that use a UUID as a primary key
|
|
*
|
|
* This helps us avoid enumeration attacks
|
|
*
|
|
*/
|
|
trait HasUUID
|
|
{
|
|
/**
|
|
* Defines the UUID field for the model.
|
|
* @return string
|
|
*/
|
|
protected static function uuidField()
|
|
{
|
|
return 'id';
|
|
}
|
|
|
|
/**
|
|
* Generate UUID v4 when creating model.
|
|
*/
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($model) {
|
|
$model->{self::uuidField()} = (string) Str::uuid();
|
|
});
|
|
}
|
|
}
|