44 lines
812 B
PHP
44 lines
812 B
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Traits\Models;
|
||
|
|
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
trait HasUuidKey
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Perform any actions required after the model boots.
|
||
|
|
*
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
protected static function booted()
|
||
|
|
{
|
||
|
|
parent::booted();
|
||
|
|
static::creating(function ($model) {
|
||
|
|
if (empty($model->{$model->getKeyName()})) {
|
||
|
|
$model->{$model->getKeyName()} = Str::uuid()->toString();
|
||
|
|
}
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the value indicating whether the IDs are incrementing.
|
||
|
|
*
|
||
|
|
* @return boolean
|
||
|
|
*/
|
||
|
|
public function getIncrementing()
|
||
|
|
{
|
||
|
|
return false;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Get the auto-incrementing key type.
|
||
|
|
*
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public function getKeyType()
|
||
|
|
{
|
||
|
|
return 'string';
|
||
|
|
}
|
||
|
|
}
|