pai-sklep/app/Models/HasUUID.php

36 lines
612 B
PHP
Raw Normal View History

2021-09-05 02:22:17 +02:00
<?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();
});
}
}