ryangjchandler/laravel-uuid

This package is abandoned and no longer maintained. No replacement package was suggested.

A small package for adding UUIDs to Eloquent models.

Fund package maintenance!
ryangjchandler

Installs: 19 965

Dependents: 0

Suggesters: 0

Security: 0

Stars: 40

Watchers: 1

Forks: 3

v2.0.0 2023-02-15 13:42 UTC

This package is auto-updated.

Last update: 2023-07-01 00:16:58 UTC


README

Warning: This package is no longer maintained. Laravel now has first-party support for generating UUIDs and ULIDs on Eloquent models, use that instead.

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 ryangjchandler/laravel-uuid

Usage

There are 2 methods for applying automatic UUID generation to your models:

1. Applying a trait

Add the RyanChandler\Uuid\Concerns\HasUuid trait to your model:

class Post extends Model
{
    use \RyanChandler\Uuid\Concerns\HasUuid;
}

This will automatically assign a time-ordered UUID to the uuid column on your model. UUIDs are generated using the Str::orderedUuid() method provided by Laravel.

If you wish to change the column that is used, you can define a uuidColumn method on your model:

class Post extends Model
{
    use \RyanChandler\Uuid\Concerns\HasUuid;

    public function uuidColumn(): string
    {
        return 'guid';
    }
}

findByUuid & findByUuidOrFail

You can use the Model::findByUuid and Model::findByUuidOrFail methods to quickly retrieve a model from it's UUID. These behave the same way as Model::find and Model::findOrFail.

2. Mass-registration in ServiceProvider

If you want to use the defaults and would like to avoid adding more traits to your model, you can mass-register your models in the boot method of a ServiceProvider.

use RyanChandler\Uuid\Uuid;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        Uuid::generateFor([
            \App\Models\Post::class,
        ]);
    }
}

Using the UUID in route definitions

If you want to use your uuid column as the default route-model binding column, you can implement the RyanChandler\Uuid\Contracts\WithUuidRouteKey contract on your model.

This will force the HasUuid trait to use the uuid column as the return value of getRouteKeyName which Laravel uses to determine how to receive a model for implicit route-model binding.

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.