Last active
March 5, 2022 07:52
-
-
Save jalallinux/4880415695a6caf458ec8346b605422b to your computer and use it in GitHub Desktop.
Laravel: Use uuid column separately
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 WithUuidColumn | |
{ | |
protected static function bootWithUuidColumn() | |
{ | |
static::creating(function (Model $model) { | |
if (! $model->getKey()) { | |
$model->{$model->getUuidKey()} = \Str::uuid()->toString(); | |
} | |
}); | |
} | |
public function getRouteKeyName() | |
{ | |
return $this->getUuidKey(); | |
} | |
public function getUuidKey(): string | |
{ | |
return 'uuid'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment