Eloquent UUID

Eloquent UUID

Eloquent UUID is a package by Matt Inamdar that is a simple drop-in solution for providing UUID (version 4) support for the IDs of your Eloquent models.

To use this package, your model needs to extend from the model class provided by this package:

<?php

namespace App\Models;

use GoldSpecDigital\LaravelEloquentUUID\Database\Eloquent\Model;

class BlogPost extends Model
{
    //
}

Note: the package also provides a model that satisfies the Authenticatable interface for the User model.

When you create a model, a UUID is automatically generated as the primary key:

// No UUID provided (automatically generated).
$model = Model::create();
echo $model->id; // abb034ae-fcdc-4200-8094-582b60a4281f

You can also specify your own UUID if you need to know the ID of the model before it’s created:

// UUID explicity provided.
$model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']);
echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e

You can learn more about this package, get full installation instructions, and view the source code on GitHub at goldspecdigital/laravel-eloquent-uuid.


Filed in: News


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Web Developer
Remote
Livesystems dooh AG
Senior Backend Engineer
Remote
64 Robots
Senior Laravel Developer
Atlanta, GA
Helium LLC
Full-Stack Developer (JS - Vue.js, PHP - Laravel)
US Remote / Telecommute
TTEC Digital
Backend Engineer
Brooklyn, NY
Stationhead
Back-End Developer
Orlando, FL
Christ for all Nations
Senior Software Developer
Nashville, TN
Bernard Health