Created
May 28, 2022 20:38
-
-
Save agungprsty/7e0b162391ef9d35b8c2012f60b4be06 to your computer and use it in GitHub Desktop.
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\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