Skip to content

Instantly share code, notes, and snippets.

@agungprsty
Created May 28, 2022 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agungprsty/7e0b162391ef9d35b8c2012f60b4be06 to your computer and use it in GitHub Desktop.
Save agungprsty/7e0b162391ef9d35b8c2012f60b4be06 to your computer and use it in GitHub Desktop.
<?php
namespace App\Traits;
use Illuminate\Support\Str;
trait Uuids
{
/**
* The "booting" method of the model, This help to magically create uuid for all new models
*
* @return void
*/
public static function boot()
{
parent::boot();
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 bool
*/
public function getIncrementing()
{
return false;
}
/**
* Get the primary key for the model.
*
* @return string
*/
public function getKeyName()
{
return 'id';
}
/**
* Get the auto-incrementing key type.
*
* @return string
*/
public function getKeyType()
{
return 'string';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment