https://github.com/riipandi/laravel-optikey
Use UUID, Ulid, or nanoid as optional or primary key in Laravel.
https://github.com/riipandi/laravel-optikey
composer laravel laravel-eloquent laravel-package nanoid ulid uuids
Last synced: 2 months ago
JSON representation
Use UUID, Ulid, or nanoid as optional or primary key in Laravel.
- Host: GitHub
- URL: https://github.com/riipandi/laravel-optikey
- Owner: riipandi
- License: mit
- Created: 2020-01-24T08:26:41.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-10T17:24:21.000Z (10 months ago)
- Last Synced: 2025-05-01T19:49:11.735Z (2 months ago)
- Topics: composer, laravel, laravel-eloquent, laravel-package, nanoid, ulid, uuids
- Language: PHP
- Homepage:
- Size: 33.2 KB
- Stars: 41
- Watchers: 1
- Forks: 8
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: license.txt
- Security: SECURITY.md
Awesome Lists containing this project
README
# Laravel OptiKey
[](https://travis-ci.com/riipandi/laravel-optikey)
[](https://github.styleci.io/repos/235965192)
[](https://packagist.org/packages/riipandi/laravel-optikey)
[](https://packagist.org/packages/riipandi/laravel-optikey)
[](https://treeware.earth)Use UUID, Ulid, or nanoid as optional or primary key in Laravel.
```bash
composer require riipandi/laravel-optikey
```This package adds a very simple trait to automatically generate a UUID, Ulid, or nanoid for your Models.
## ✌️ Using as Secondary Key
### 1. Update your schemas
First, you need to add an extra column in your migration. For example:
```sh
php artisan make:migration AddOptikeyToUsersTable
``````php
// If using UUID for the key
$table->uuid('uid')->after('id')->unique()->index();// If using nanoid or ulid for the key
$table->string('uid')->after('id')->unique()->index();
```Sample migration:
```php
string('uid', 26)->index()->after('id');
});// Prefill uid column in users table
Schema::table('users', function (Blueprint $table) {
$results = DB::table('users')->select('id')->get();
foreach ($results as $result) {
$ulid = \Ulid\Ulid::generate($lowercase = true); // Generate new lowercase Ulid
$generated = 'user_'.$ulid; // this is the generated value with optional prefix
DB::table('users')->where('id', $result->id)->update(['uid' => $generated]);
}
});// Set uid column as unique
Schema::table('users', function (Blueprint $table) {
$table->unique('uid');
});
}public function down()
{
Schema::table('users', function (Blueprint $table) {
$table->dropColumn('uid');
});
}
}
```### 2. Add the trait
Add the trait to your model (pick one between `HasUuidKey`, `HasUlidKey`, or `HasNanoidKey`):
```php
first();
```Or, using static find method:
```php
\App\User::findByOptiKey('xxxxxxxxxxx');
```## ☝️ Using as Primary Key
You need to change the primary key field type in your migration. For example:
```php
$table->uuid('id')->primary(); // for UUID
$table->string('id', 26)->primary(); // for Ulid
$table->string('id', 16)->primary(); // for nanoid
```Add second trait to use as primary key:
```php
Copyrights in this project are retained by their contributors.
No copyright assignment is required to contribute to this project.[choosealicense]:https://choosealicense.com/licenses/mit/
This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/riipandi/laravel-optikey) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats.
Please see [license file](./license.txt) for more information.