riipandi/laravel-optikey

Use UUID or Ulid as optional or primary key in Laravel.

v1.2 2020-03-03 23:57 UTC

This package is auto-updated.

Last update: 2020-11-04 01:31:35 UTC


README

Build Status StyleCI Latest Stable Version Total Downloads

Use UUID or Ulid as optional or primary key in Laravel.

composer require riipandi/laravel-optikey

This package adds a very simple trait to automatically generate a UUID or Ulid for your Models.

Quick Start

Update your schemas

First, you need to add uuid or ulid column in your migration. For example:

php artisan make:migration AddUuidColumnToUsersTable

In this case you will use UUID as secondary key:

$table->uuid('uuid')->after('id')->unique()->index();

In this case you will use UUID as primary key:

$table->uuid('id')->primary();

Sample migration:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUlidColumnToUsersTable extends Migration
{
    public function up()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->string('ulid', 26)->unique()->index()->after('id');
        });
    }
    
    public function down()
    {
        Schema::table('users', function (Blueprint $table) {
            $table->dropColumn('ulid');
        });
    }
}

Using UUID

Add the "\Riipandi\LaravelOptiKey\Traits\HasUuidKey;" trait to your model:

<?php

namespace App;

use Riipandi\LaravelOptiKey\Traits\HasUuidKey;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasUuidKey;
}

If your column name is not "uuid", simply add a new property to your model named "uuidFieldName":

protected $uuidFieldName = 'unique_id';

This trait also adds a scope:

\App\User::byUuid('uuid')->first();

And static find method:

\App\User::findByUuid('uuid')

A second trait is available if you use your UUIDs as primary keys:

<?php

namespace App;

use Riipandi\LaravelOptiKey\Traits\HasUuidKey;
use Riipandi\LaravelOptiKey\Traits\UuidAsPrimaryKey;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasUuidKey, UuidAsPrimaryKey;
}

Using Ulid

Add the "\Riipandi\LaravelOptiKey\Traits\HasUlidKey;" trait to your model:

<?php

namespace App;

use Riipandi\LaravelOptiKey\Traits\HasUlidKey;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasUlidKey;
}

If your column name is not "ulid", simply add a new property to your model named "ulidFieldName":

protected $ulidFieldName = 'unique_id';

This trait also adds a scope:

\App\User::byUlid('ulid')->first();

And static find method:

\App\User::findByUlid('ulid')

A second trait is available if you use your Ulids as primary keys:

<?php

namespace App;

use Riipandi\LaravelOptiKey\Traits\HasUlidKey;
use Riipandi\LaravelOptiKey\Traits\UlidAsPrimaryKey;
use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    use HasUlidKey, UlidAsPrimaryKey;
}

It simply tells Laravel that your primary key isn't an auto-incrementing integer, so it will treat the value correctly.

License

Copyright 2020 - Aris Ripandi

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at: http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.