Skip to content

Instantly share code, notes, and snippets.

@alexalouit
Created November 13, 2019 06:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexalouit/cc36ad1d49f8c156a59a46f434fe7451 to your computer and use it in GitHub Desktop.
Save alexalouit/cc36ad1d49f8c156a59a46f434fe7451 to your computer and use it in GitHub Desktop.
Laravel/Lumen use uuid as id
# lumen:
$ composer require ramsey/uuid
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class ModelUuid extends Model
{
/**
* Override default id
*
* @return void
*/
public static function boot()
{
parent::boot();
self::creating(function ($model) {
$model->id = 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';
}
}
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Post extends ModelUuid
{
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment