pai-sklep/app/Models/HasUUID.php

47 lines
779 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
{
2021-10-28 11:19:27 +02:00
protected static function getPrimaryKey()
{
return self::uuidField();
}
2021-09-05 02:22:17 +02:00
/**
* Defines the UUID field for the model.
* @return string
*/
protected static function uuidField()
{
2021-10-28 11:19:27 +02:00
return 'uuid';
}
public function getIncrementing()
{
return false;
2021-09-05 02:22:17 +02:00
}
/**
* Generate UUID v4 when creating model.
*/
protected static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->{self::uuidField()} = (string) Str::uuid();
});
}
}