Skip to content

Instantly share code, notes, and snippets.

@jalallinux
Last active March 5, 2022 07:53
Show Gist options
  • Save jalallinux/a9499046d7e1d7d7009ad252a953cf45 to your computer and use it in GitHub Desktop.
Save jalallinux/a9499046d7e1d7d7009ad252a953cf45 to your computer and use it in GitHub Desktop.
Laravel: UseUuid trait
<?php
namespace App\Models\Traits;
use Illuminate\Database\Eloquent\Model;
trait UseUuidPrimary
{
protected static function bootUseUuidPrimary()
{
static::creating(function (Model $model) {
if (! $model->getKey()) {
$model->{$model->getKeyName()} = \Str::uuid()->toString();
}
});
}
public function getIncrementing()
{
return false;
}
public function getKeyType()
{
return 'string';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment