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

Overview

Laravel OptiKey

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 "optiKeyFieldName":

protected $optiKeyFieldName = '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 "optiKeyFieldName":

protected $optiKeyFieldName = '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.

Copyright

This project is licensed under MIT: https://aris.mit-license.org/

Copyrights in this project are retained by their contributors. No copyright assignment is required to contribute to this project. Please see license file for more information.

You might also like...
A Laravel package to retrieve key management from AWS Secrets Manager

A Laravel package to retrieve key management from AWS Secrets Manager Communication via AWS Secrets Manager may incur unnecessary charges. So we devel

A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.
A simple to use query builder for the jQuery QueryBuilder plugin for use with Laravel.

QueryBuilderParser Status Label Status Value Build Insights Code Climate Test Coverage QueryBuilderParser is designed mainly to be used inside Laravel

A Laravel package that allows you to use multiple
A Laravel package that allows you to use multiple ".env" files in a precedent manner. Use ".env" files per domain (multi-tentant)!

Laravel Multi ENVs Use multiple .envs files and have a chain of precedence for the environment variables in these different .envs files. Use the .env

A super simple package allowing for use MySQL 'USE INDEX' and 'FORCE INDEX' statements.
A super simple package allowing for use MySQL 'USE INDEX' and 'FORCE INDEX' statements.

Laravel MySQL Use Index Scope A super simple package allowing for use MySQL USE INDEX and FORCE INDEX statements. Requirements PHP ^7.4 | ^8.0 Laravel

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel router extension to easily use Laravel's paginator without the query string

🚨 THIS PACKAGE HAS BEEN ABANDONED 🚨 We don't use this package anymore in our own projects and cannot justify the time needed to maintain it anymore.

A Laravel chat package. You can use this package to create a chat/messaging Laravel application.
A Laravel chat package. You can use this package to create a chat/messaging Laravel application.

Chat Create a Chat application for your multiple Models Table of Contents Click to expand Introduction Installation Usage Adding the ability to partic

Laravel-FCM is an easy to use package working with both Laravel and Lumen for sending push notification with Firebase Cloud Messaging (FCM).

Laravel-FCM Introduction Laravel-FCM is an easy to use package working with both Laravel and Lumen for sending push notification with Firebase Cloud M

Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS.

Nebula Nebula is a minimalistic and easy to use administration tool for Laravel applications, made with Laravel, Alpine.js, and Tailwind CSS. Nebula m

Releases(v2.1)
Owner
Aris Ripandi
Crafting webapps, solving business problems with technology. Open Source enthusiast. Life is short - do something that matters!
Aris Ripandi
Laravel package to generate and to validate a UUID according to the RFC 4122 standard. Only support for version 1, 3, 4 and 5 UUID are built-in.

Laravel Uuid Laravel package to generate and to validate a universally unique identifier (UUID) according to the RFC 4122 standard. Support for versio

Christoph Kempen 1.7k Dec 28, 2022
Use auto generated UUID slugs to identify and retrieve your Eloquent models.

Laravel Eloquent UUID slug Summary About Features Requirements Installation Examples Compatibility table Alternatives Tests About By default, when get

Khalyomede 25 Dec 14, 2022
Use eloquent joins in Laravel way, with composite key support.

Eloquent Power Joins with Compoships Support This package is an Eloquent Power Joins extension to support Compoships. You can now use joins in Laravel

Kit Loong 13 Dec 23, 2022
A laravel package to attach uuid to model classes

Laravel Model UUID A simple package to generate model uuid for laravel models Installation Require the package using composer: composer require touhid

null 10 Jan 20, 2022
Generate UUID for a Laravel Eloquent model attribute

Generate a UUIDv4 for the primary key or any other attribute on an Eloquent model.

Alex Bouma 4 Mar 1, 2022
This package provides a trait that will generate a unique uuid when saving any Eloquent model.

Generate slugs when saving Eloquent models This package provides a trait that will generate a unique uuid when saving any Eloquent model. $model = new

Abdul Kudus 2 Oct 14, 2021
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.

Introduction A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box

GoldSpec Digital 501 Jan 4, 2023
A simple laravel package to handle multiple key based model route binding

Laravel Model UUID A simple package to handle the multiple key/column based route model binding for laravel package Installation Require the package u

null 13 Mar 2, 2022
⚙️Laravel Nova Resource for a simple key/value typed setting

Laravel Nova Resource for a simple key/value typed setting Administer your Laravel Simple Setting in Nova Pre-requisites This Nova resource package re

elipZis 5 Nov 7, 2022
⚙️Simple key/value typed settings for your Laravel app with synchronized json export

Simple key/value typed settings for your Laravel app Create, store and use key/value settings, typed from numbers over dates to array, cached for quic

elipZis 8 Jan 7, 2023