Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mawuva/laravel-model-uuid
Easily work with UUIDs in your Laravel models
https://github.com/mawuva/laravel-model-uuid
Last synced: 7 days ago
JSON representation
Easily work with UUIDs in your Laravel models
- Host: GitHub
- URL: https://github.com/mawuva/laravel-model-uuid
- Owner: mawuva
- License: mit
- Created: 2021-06-16T14:30:05.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-03-14T01:16:17.000Z (8 months ago)
- Last Synced: 2024-03-15T02:24:36.533Z (8 months ago)
- Language: PHP
- Size: 28.3 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Model UUIDs
[![Latest Version on Packagist](https://img.shields.io/packagist/v/mawuekom/laravel-model-uuid.svg?style=flat-square)](https://packagist.org/packages/mawuekom/laravel-model-uuid)
[![Total Downloads](https://img.shields.io/packagist/dt/mawuekom/laravel-model-uuid.svg?style=flat-square)](https://packagist.org/packages/mawuekom/laravel-model-uuid)
![GitHub Actions](https://github.com/mawuva/laravel-model-uuid/actions/workflows/main.yml/badge.svg)A simple solution to easily work with UUIDs in your Laravel models.
This package provides a way to generate and work with [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122) version 1, 2, 3, 4, and 5 universally unique identifiers (UUID). It also supports optional and non-standard features, such as [version 6 UUIDs], GUIDs, and other approaches for encoding/decoding UUIDs.
It based on [ramsey/uuid](https://uuid.ramsey.dev/en/latest/index.html) package.
Take a look on this for more details.## What Is a UUID?
A universally unique identifier, or UUID, is a 128-bit unsigned integer, usually represented as a hexadecimal string split into five groups with dashes. The most widely-known and used types of UUIDs are defined by [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122).
A UUID, when encoded in hexadecimal string format, looks like:
```text
ebb5c735-0308-4edc-9aea-8a270aebfe15
```The probability of duplicating a UUID is close to zero, so they are a great choice for generating unique identifiers in distributed systems.
UUIDs can also be stored in binary format, as a string of 16 bytes.
It is suggested to use UUIDs in your responses instead of IDs, which are
generally enumerable.This will help mitigate some forms of enumeration attacks.
**Note**: This package explicitly does not disable auto-incrementing on your Eloquent models. In terms of database indexing, it is generally more efficient to use auto-incrementing integers for your internal querying. Indexing your uuid column will make lookups against that column fast, without impacting queries between related models.
## Installation
You can install the package via composer:
```bash
composer require mawuekom/laravel-model-uuid
```## Usage
To use this package, you simply need to import and use the trait within your Eloquent models.
```php
first();// Find multiple posts with the default (uuid) column name
$post = Post::whereUuid([$first, $second])->get();// Find a specific post with a custom column name
$post = Post::whereUuid($uuid, 'custom_column')->first();// Find multiple posts with a custom column name
$post = Post::whereUuid([$first, $second], 'custom_column')->get();
```It also have methods
- `getIdFromUuid` : Retrieve auto-incrementing ID from Uuid
- `loadFromUuid` : Retrieve model data from UuidThere is also `ValidatesUuid` trait which as his name says validate incoming UUID before make model queries.
You can use it in your Controller too :
```php
validatesUuid('custom_column', $uuid, Post::class);
}
}
```There is also a useful helper function `is_the_given_id_a_uuid` that return `true` if the given id is a uuid or `false` if not.
```php