Skip to content

Instantly share code, notes, and snippets.

@jalallinux
Last active March 5, 2022 07:52
Show Gist options
  • Save jalallinux/4880415695a6caf458ec8346b605422b to your computer and use it in GitHub Desktop.
Save jalallinux/4880415695a6caf458ec8346b605422b to your computer and use it in GitHub Desktop.
Laravel: Use uuid column separately
<?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