pai-sklep/app/Models/HasUUID.php
2021-11-14 14:51:49 +01:00

45 lines
833 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
{
protected static function getPrimaryKey()
{
return self::uuidField();
}
/**
* Defines the UUID field for the model.
* @return string
*/
protected static function uuidField()
{
return 'uuid';
}
public function getIncrementing()
{
return false;
}
/**
* Generate UUID v4 when creating model.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{self::uuidField()} = (string) Str::uuid();
});
}
}