pai-sklep/app/Models/HasUUID.php

46 lines
833 B
PHP
Raw Normal View History

2021-09-05 02:22:17 +02:00
<?php
namespace App\Models;
use Illuminate\Support\Str;
2021-11-14 14:51:49 +01:00
/*
2021-09-05 02:22:17 +02:00
* A trait for all models that use a UUID as a primary key
2021-11-14 14:51:49 +01:00
*
2021-09-05 02:22:17 +02:00
* This helps us avoid enumeration attacks
2021-11-14 14:51:49 +01:00
*
2021-09-05 02:22:17 +02:00
*/
trait HasUUID
2021-11-14 14:51:49 +01:00
{
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();
});
}
}