laravel-eloquent-uuid | simple drop-in solution for providing UUID support | Identity Management library
kandi X-RAY | laravel-eloquent-uuid Summary
kandi X-RAY | laravel-eloquent-uuid Summary
A simple drop-in solution for providing UUID support for the IDs of your Eloquent models. Both v1 and v4 IDs are supported out of the box, however should you need v3 or v5 support, you can easily add this in.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Bootstrap event .
- Generate UUID .
- Get options .
- Bootstrap uuid .
- Get option .
- Get model stub .
- Check if key is uuid
- Returns uuid version .
laravel-eloquent-uuid Key Features
laravel-eloquent-uuid Examples and Code Snippets
Community Discussions
Trending Discussions on laravel-eloquent-uuid
QUESTION
Auto-incrementing integers cannot be used for primary keys in distributed database topologies in which the potential for conflicts (collisions) exists.
The extant body of literature regarding the subject of UUIDs vs. auto-incrementing integers is vast, and the fundamental precepts are widely understood. Yet, at the same time, no single, comprehensive explanation of how to achieve this in Laravel, with support for Eloquent Models and relationships, seems to exist.
The following article is worthwhile and explains the performance overhead incurred for storing primary keys in VARCHAR(36)
/CHAR(36)
vs the 4/8-byte integer normally used for auto-incrementing keys. We should heed this advice (especially the post-publication corrections that the author notes throughout):
https://tomharrisonjr.com/uuid-or-guid-as-primary-keys-be-careful-7b2aa3dcb439
Equally valuable is the commentary that stems from the discussion, which is extensive:
https://news.ycombinator.com/item?id=14523523
The following article explains how to implement primary keys using UUIDs in Laravel Eloquent models, but it falls short of explaining how to implement the same for Eloquent relationships, e.g., Many-to-Many with "pivot tables" (per Laravel parlance).
https://medium.com/@steveazz/setting-up-uuids-in-laravel-5-552412db2088
Others have asked similar questions, such as Laravel eloquent UUID in a pivot table , but in that instance, the asker is generating the UUID to be inserted in the pivot table using a MySQL trigger, which I would prefer to avoid in favor of a purely-Eloquent approach.
Another similar question is asked at How To Cast Eloquent Pivot Parameters? , but the crux of the question is how to cast pivot attributes, not how to generate custom values for the ID column upon attaching or synching relationships.
To be clear, we can achieve this easily by passing the optional array argument to the attach()
method:
ANSWER
Answered 2018-Jun-08 at 18:26Disclaimer: This is a work-in-progress. As yet, this technique focuses only on Many-to-Many Eloquent relationships, and not the more exotic types, such as Has-Many-Through or Polymorphics.
Current as of Laravel v5.5.* UUID Generation Packages for LaravelBefore we get started, we need a mechanism by which to generate UUIDs.
The most popular package for UUID generation is as follows:
https://github.com/webpatser/laravel-uuid
Implemeting UUIDs for Eloquent ModelsThe ability for a model to use a UUID as its primary key may be conferred either by extending Laravel's base Model class, or by implementing a trait. Each approach has its strengths and weaknesses, and because Steve Azzopardi's medium.com article (cited above) already explains the trait method (though, it predates Eloquent's $keyType = 'string';
property), I'll demonstrate the Model extension approach, which, of course, may be adapted to a trait with ease.
Whether we use a model or a trait, the crucial aspects are $incrementing = false;
and protected $keyType = 'string';
. While extending the base Model class imposes limitations due to PHP's single-inheritance design, it eliminates the need to include these two crucial properties in every model that should utilize a UUID primary key. By contrast, when using a trait, forgetting to include both of these in every model that uses the trait will cause failures.
The base UUID model class:
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install laravel-eloquent-uuid
PHP requires the Visual C runtime (CRT). The Microsoft Visual C++ Redistributable for Visual Studio 2019 is suitable for all these PHP versions, see visualstudio.microsoft.com. You MUST download the x86 CRT for PHP x86 builds and the x64 CRT for PHP x64 builds. The CRT installer supports the /quiet and /norestart command-line switches, so you can also script it.
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page