A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.
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, however should you need
v3 or v5 support, you can easily add this in.
Reference the table below for the correct version to use in conjunction with the version of Laravel you have installed:
| Laravel | This package |
|---|---|
v5.8.* |
v1.* |
v6.* |
v6.* |
v7.* |
v7.* |
v8.* |
v8.* |
v9.* |
v9.* |
v10.* |
v10.* |
You can install the package via composer:
composer require goldspecdigital/laravel-eloquent-uuid:^10.0There are two ways to use this package:
When creating a Eloquent model, instead of extending the standard Laravel model class, extend from the model class provided by this package:
id; // abb034ae-fcdc-4200-8094-582b60a4281f
// UUID explicity provided.
$model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']);
echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76eBy default, v4 UUIDs will be used for your models. However, you can also
specify v1 UUIDs to be used by setting the following property/method on your
model:
toString();
}
}In addition of the make:model artisan command, you will now have access to
uuid:make:model which has all the functionality of the standard make:model
command (with exception of not being able to create a pivot model):
php artisan uuid:make:model Models/Post --allThe default primary ID column used in migrations will not work with UUID primary keys, as the default column type is an unsigned integer. UUIDs are 36 character strings so we must specify this in our migrations:
uuid('id')->primary();
});
}
}
class CreatePostsTable extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('posts', function (Blueprint $table): void {
// Primary key.
$table->uuid('id')->primary();
// Foreign key.
$table->uuid('user_id');
$table->foreign('user_id')->references('id')->on('users');
});
}
}To run the test suite you can use the following command:
composer testPlease read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.
We use SemVer for versioning. For the versions available, see the tags on this repository.
See also the list of contributors who participated in this project.
This project is licensed under the MIT License - see the LICENSE.md file for details.
No open issues yet, or sync has not completed.