ldc-com-app-bo/app/Traits/Models/HasUuidKey.php

44 lines
812 B
PHP
Raw Normal View History

<?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';
}
}