ryancco/laravel-uuid-models

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

Trait for working with Eloquent models with UUID route keys

2.0 2021-04-24 06:17 UTC

This package is auto-updated.

Last update: 2023-08-24 11:43:11 UTC


README

Latest Version on Packagist Build Status

Laravel UUID Models

This package provides a simple trait to be used on Laravel Eloquent Models to provide a drop-in solution for UUID route keys (rather than auto-incrementing ID's).

Installation

The only supported automated installation is via Composer

composer require ryancco/laravel-uuid-models

Usage

Once the package has been installed, you can add the trait to any Eloquent Models you wish to have UUID route keys.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ryancco\HasUuidRouteKey\HasUuidRouteKey;

class Post extends Model
{
    use HasUuidRouteKey;
}

Next, add a column to these model's database tables to store the UUID; here's an example migration:

<?php

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

class AddUuidColumnToPostsTable extends Migration
{
    public function up(): void
    {
        Schema::table('posts', static function (Blueprint $table) {
            $table->uuid('uuid');
        });
    }
}

Now you can use the configured UUID attribute as a route key just as you would with an auto-incrementing ID route key.

Routes

<?php
// Route registration works exactly as it had with models routed by the "id" attribute

// "binded" routes
Route::get('posts/{post}', 'PostController@show');

// resourceful routes
Route::resource('posts', 'PostController');

// ...

Tests

<?php

namespace Tests\Feature;

use App\Models\Post;
use Tests\TestCase;

class PostsControllerTest extends TestCase
{
    public function testAdminsHavePermissionToViewPrivatePosts(): void
    {
        $post = factory(Post::class)->state('private')->create();

        $this->get(route('posts.view', $post))->assertOk();
        // route('posts.view', $post->uuid) - localhost/posts/94205252-7c44-4e5b-ad75-682ac81fea84
    }
}

Configuration

By default, the route key is named 'uuid', but this can be configured to whatever you would like.

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Ryancco\HasUuidRouteKey\HasUuidRouteKey;

class Post extends Model
{
    use HasUuidRouteKey;

    public function getRouteKeyName() : string
    {
        return 'something-else';
    }
}

Caveats

One thing to take note of, is that the UUID generation is triggered when the model event "creating" is triggered. The most important thing to keep in mind, and why this is the case, is that the UUID will not be generated when instantiating a model via the new keyword* but rather once it has been persisted to (or retrieved from) the database.

If the situation arises where you need the UUID generated before the "creating" model event is fired, you can manually call the following method:

<?php

$post = new App\Models\Post();
$post->generateUuidRouteKey();

All routes will work assuming they're generated with the route() helper.

Contributing

Please report any problems by creating an issue and pull request (encouraged, but not required).