vanthao03596/laravel-ulid

Laravel package for ULID (Universally Unique Lexicographically Sortable Identifier)

0.1 2021-08-24 13:11 UTC

This package is auto-updated.

Last update: 2024-03-24 18:58:43 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Installation

You can install the package via composer:

composer require vanthao03596/laravel-ulid

Usage

Generate uld from Str support class

// Default ulid generator with the current timestamp & lowercase string
Str::ulid(); // 01FDRXQ57VR4K4RASPT9NPAQC0
// Default ulid generator with the current timestamp & uppercase string
Str::ulid(false); // 01fdrxpmg9njp20z3km461fgax
// Default ulid generator with the given datetime & uppercase string
Str::ulid(false, Carbon::now()->subHour()) // 01FDRTD2K8Z664S24X606N14KD

Simply declare a ulid column type in your migration files.

Schema::create('foos', function (Blueprint $table) {
    $table->ulid('id')->primary(); // adds primary ulid column 
    $table->foreignUlid('user_id')->constrained()->cascadeOnDelete(); // adds ulid foreignkey
    $table->ulidMorphs('taggable'); // adds ulid morphs
    $table->nullableUlidMorphs('likeable'); // adds nullable ulid morphs
});

Auto generate ulid column in your model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Vanthao03596\LaravelUlid\GeneratesUlid;

class User extends Model
{
    use GeneratesUlid;
}

Default column is ulid. If you wish to use a custom column name, for example if you want your primary custom_column column to be a ULID, you can define a ulidColumn method in your model.

class User extends Model
{
    public function ulidColumn(): string
    {
        return 'custom_column';
    }
}

You can have multiple ULID columns in each table by specifying an array in the ulidColumns method.

class User extends Model
{
    public function ulidColumns(): array
    {
        return ['ulid', 'custom_column'];
    }
}

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.