pai-sklep/app/Models/HasUUID.php
2021-09-10 09:41:55 +02:00

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();
});
}
}