PHPackages                             riipandi/laravel-optikey - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. riipandi/laravel-optikey

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

riipandi/laravel-optikey
========================

Use UUID, Ulid, or nanoid as optional or primary key in Laravel.

v2.3(1y ago)429.1k8[1 PRs](https://github.com/riipandi/laravel-optikey/pulls)MITPHPPHP ^7.3|^8.0

Since Jan 24Pushed 1y ago1 watchersCompare

[ Source](https://github.com/riipandi/laravel-optikey)[ Packagist](https://packagist.org/packages/riipandi/laravel-optikey)[ RSS](/packages/riipandi-laravel-optikey/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (9)Dependencies (4)Versions (10)Used By (0)

Laravel OptiKey
===============

[](#laravel-optikey)

[![Build Status](https://camo.githubusercontent.com/a31c3fdb83298b9f8ef6afa04502f1ce02153cd7712b53924200d5e6a38574ba/68747470733a2f2f7472617669732d63692e636f6d2f72696970616e64692f6c61726176656c2d6f7074696b65792e7376673f6272616e63683d6d6173746572)](https://travis-ci.com/riipandi/laravel-optikey)[![StyleCI](https://camo.githubusercontent.com/c2c03e67db69fa34f427231f1136ba71e695f0ce089180b054a8bb3178a7add1/68747470733a2f2f6769746875622e7374796c6563692e696f2f7265706f732f3233353936353139322f736869656c643f6272616e63683d6d6173746572)](https://github.styleci.io/repos/235965192)[![Latest Stable Version](https://camo.githubusercontent.com/41e0d837d8f87a55c72546003e9b2ab9b05cea4a61595570cdb2f0ce0b6a2a9b/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72696970616e64692f6c61726176656c2d6f7074696b65792e7376673f7374796c653d666c6174)](https://packagist.org/packages/riipandi/laravel-optikey)[![Total Downloads](https://camo.githubusercontent.com/b1e880b5f9087de5ce0b2f4b13a000039b41267b71940b3c2ba71f5530176791/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72696970616e64692f6c61726176656c2d6f7074696b65792e7376673f7374796c653d666c6174)](https://packagist.org/packages/riipandi/laravel-optikey)[![Treeware](https://camo.githubusercontent.com/aef9384539fd5e8ee99698a94694323f0c973fbb7c56096f65c481dac3fed691/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f64796e616d69632f6a736f6e3f636f6c6f723d627269676874677265656e266c6162656c3d54726565776172652671756572793d2532342e746f74616c2675726c3d68747470732533412532462532467075626c69632e6f66667365742e6561727468253246757365727325324674726565776172652532467472656573)](https://treeware.earth)

Use UUID, Ulid, or nanoid as optional or primary key in Laravel.

```
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
-------------------------

[](#️-using-as-secondary-key)

### 1. Update your schemas

[](#1-update-your-schemas)

First, you need to add an extra column in your migration. For example:

```
php artisan make:migration AddOptikeyToUsersTable
```

```
// 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:

```
