Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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: 6 days 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 (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2024-09-10T17:24:21.000Z (about 2 months ago)
- Last Synced: 2024-10-03T12:31:18.685Z (about 1 month ago)
- Topics: composer, laravel, laravel-eloquent, laravel-package, nanoid, ulid, uuids
- Language: PHP
- Homepage:
- Size: 33.2 KB
- Stars: 40
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: license.txt
- Security: SECURITY.md
Awesome Lists containing this project
README
# Laravel OptiKey
[![Build Status](https://travis-ci.com/riipandi/laravel-optikey.svg?branch=master)](https://travis-ci.com/riipandi/laravel-optikey)
[![StyleCI](https://github.styleci.io/repos/235965192/shield?branch=master)](https://github.styleci.io/repos/235965192)
[![Latest Stable Version](http://img.shields.io/packagist/v/riipandi/laravel-optikey.svg?style=flat)](https://packagist.org/packages/riipandi/laravel-optikey)
[![Total Downloads](http://img.shields.io/packagist/dt/riipandi/laravel-optikey.svg?style=flat)](https://packagist.org/packages/riipandi/laravel-optikey)
[![Treeware](https://img.shields.io/badge/dynamic/json?color=brightgreen&label=Treeware&query=%24.total&url=https%3A%2F%2Fpublic.offset.earth%2Fusers%2Ftreeware%2Ftrees)](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.