eloquent-uuid | flexible Laravel package that adds support | Identity Management library
kandi X-RAY | eloquent-uuid Summary
kandi X-RAY | eloquent-uuid Summary
A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. is a 36 character long identifier made up of 32 alphanumeric characters with four hyphens in amongst it. For example:123E4567-E89b-12D3-A456-426655440000 containing letters and numbers. that will uniquely identify something. you can read more here.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Boot the has - uuid column .
- Generate a unique UUID
- Find by uuid .
- Validate UUID version
- Assert that the model has uuid column .
- Validate a UUID .
- Get uuid .
- Set UUID .
- Get the UUID string .
- Get the route key .
eloquent-uuid Key Features
eloquent-uuid Examples and Code Snippets
Community Discussions
Trending Discussions on 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 eloquent-uuid
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