PHPackages                             foodticket/laravel-efficient-uuid - 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. [Database &amp; ORM](/categories/database)
4. /
5. foodticket/laravel-efficient-uuid

ActiveUtility[Database &amp; ORM](/categories/database)

foodticket/laravel-efficient-uuid
=================================

A package to extend Laravel migrations adding a more efficient storage of UUID fields in your database

5.0.1(2y ago)093MITPHPPHP ^8.1 || ^8.2

Since Apr 15Pushed 2y agoCompare

[ Source](https://github.com/food-ticket/laravel-efficient-uuid)[ Packagist](https://packagist.org/packages/foodticket/laravel-efficient-uuid)[ RSS](/packages/foodticket-laravel-efficient-uuid/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (1)Dependencies (9)Versions (3)Used By (0)

Laravel Efficient UUIDs
=======================

[](#laravel-efficient-uuids)

[![Build Status](https://github.com/michaeldyrynda/laravel-nullable-fields/workflows/run-tests/badge.svg)](https://github.com/michaeldyrynda/laravel-nullable-fields/actions?query=workflow%3Arun-tests)[![Latest Stable Version](https://camo.githubusercontent.com/4e2f83a8b2f6b1941a6d18d380ba4bb304efa64111fba15bacd6df65b6049755/68747470733a2f2f706f7365722e707567782e6f72672f647972796e64612f6c61726176656c2d656666696369656e742d757569642f762f737461626c65)](https://packagist.org/packages/dyrynda/laravel-efficient-uuid)[![Total Downloads](https://camo.githubusercontent.com/9b3813794c3f13b4b6b4eb201b363a078e469ab64f0a28f07cc97296839cf5a1/68747470733a2f2f706f7365722e707567782e6f72672f647972796e64612f6c61726176656c2d656666696369656e742d757569642f646f776e6c6f616473)](https://packagist.org/packages/dyrynda/laravel-efficient-uuid)[![License](https://camo.githubusercontent.com/271ebe94672b445a88e3580520b0ee8daf44308834e64a6493c7db049bd2a6d4/68747470733a2f2f706f7365722e707567782e6f72672f647972796e64612f6c61726176656c2d656666696369656e742d757569642f6c6963656e7365)](https://packagist.org/packages/dyrynda/laravel-efficient-uuid)[![Buy us a tree](https://camo.githubusercontent.com/130148911f548b001b2ac68a32c0a06559977ca60ada3bf480c72ae4ea093175/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f54726565776172652d2546302539462538432542332d6c69676874677265656e)](https://plant.treeware.earth/michaeldyrynda/laravel-efficient-uuid)

Introduction
------------

[](#introduction)

This package extends the default grammar file for the given MySQL connection adding an `efficientUuid` blueprint method that creates a `binary(16)` field.

As of 3.0, this package *no longer overrides* Laravel's default `uuid` method, but rather adds a separate `efficientUuid` field, due to compatibility issues with Laravel Telescope ([\#11](https://github.com/user/repo/issues/11)).

As of 4.0, this package uses a [custom cast](https://laravel.com/docs/7.x/eloquent-mutators#custom-casts) to provide casting functionality into your models.

> **Note**: This package purposely does not use [package discovery](https://laravel.com/docs/5.8/packages#package-discovery), as it makes changes to the MySQL schema file, which is something you should explicitly enable.

MySQL, SQLite, and PostgreSQL are the only supported connection types, although I welcome any pull requests to implement this functionality for other database drivers.

Note that `doctrine/dbal` does not appear to support changing existing `uuid` fields, and doing so would cause your existing values to be truncated in any event.

For more information, check out [this post](https://www.percona.com/blog/2014/12/19/store-uuid-optimized-way/) on storing and working with UUID in an optimised manner.

Using UUIDs in Laravel is made super simple in combination with [laravel-model-uuid](https://github.com/michaeldyrynda/laravel-model-uuid). Note that when using `laravel-model-uuid`, if you are not casting your UUIDs or calling the query builder directly, you'll need to use the `getBytes` method when setting the UUID on the database, otherwise your values will be truncated. Depending on your MySQL/MariaDB configuration, this may lead to application errors due to strict settings. See ([\#1](https://github.com/user/repo/issues/1)) for more information.

This package is installed via [Composer](https://getcomposer.org/). To install, run the following command.

```
composer require dyrynda/laravel-efficient-uuid
```

Register the service provider in your `config/app.php` configuration file:

```
'providers' => [
    ...
    Dyrynda\Database\LaravelEfficientUuidServiceProvider::class,
],
```

### Migrations

[](#migrations)

There is nothing special needed for this to function, simply declare a `uuid` column type in your migration files. If you plan on querying against the UUID column, it is recommended that you index the column, but avoid making it the primary key.

```
Schema::create('posts', function (Blueprint $table) {
    $table->increments('id');
    $table->efficientUuid('uuid')->unique();
    $table->string('title');
    $table->text('body');
    $table->timestamps();
});
```

### Casting

[](#casting)

You will need to add a cast to your model when using [laravel-model-uuid](https://github.com/michaeldyrynda/laravel-model-uuid) in order to correctly set and retrieve UUID from your MySQL database with binary fields.

```
