Last active
March 5, 2022 07:53
-
-
Save jalallinux/a9499046d7e1d7d7009ad252a953cf45 to your computer and use it in GitHub Desktop.
Laravel: UseUuid trait
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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