What is this package?

This is a simple package to automatically add a unique id (str_random()) to your Eloquent models when they are created. It will query the database to only use a unique ID that really is unique.

Blog Etc - laravel blog package
View on Github View on Packagist

Install guide

It is realy just a case of installing it via composer, and then using the trait via use UniqueId.

  1. Run the following command: composer require webdevetc/laraveluniqueidetc && php artisan vendor:publish --tag=uniqueidetc_config
  2. Then use the trait in your Eloquent model classes: use WebDevEtc\LaravelUniqueIdEtc\Traits\UniqueId;
  3. Additional options can be found in /config/uniqueid.php
  4. You will probably also want to update your controllers (or repos) to do things like where("unique_id",$some_id), and/or update your model's getRouteKeyName() method.

How to create the database migration

You must also create a db migration to add the unique id field. By default this is `unique_id`.

For example, to add a unique_id field to your User model (which uses the `users` table):

php artisan make:migration --table='users'

And within the migration's up method, add this:


And in the down() method:


More Info

Tests can be found in the /tests directory. It assumes that you are running the tests within a Laravel installation.

Please contact me with any bugs/issues and I'll get them fixed.

There is a chance that between generating the ID (and checking if it exists in the database) and the insert into... SQL query gets executed that another row has that ID. However, if you make sure that your DB column has a unique index it should avoid any terrible problems if this (rare) case happens.