emadadly / laravel-uuid Goto Github PK
View Code? Open in Web Editor NEWlaravel uuid a simple, automatic UUID generator for any model based on Laravel
License: MIT License
laravel uuid a simple, automatic UUID generator for any model based on Laravel
License: MIT License
Hi,
I am using the package to generate the uuid only for showing it in the routes post/:uuid
but wanna keep the auto increment(PK) column id
as it is and to be able to store it in related models as FK. I wonder if there is a clean way to get the uuid from the request and replace it with the id before creating the new record(for the case when it is gonna be inserted in the related model post_id
)?
Cheers!
With the uuid isn't there a chance to have users with the same uuid if you have a large database?
Also I've been reading allot about uuid & people have different opinions on if you should keep the auto increment id in the database.
People also bring up about a performance issue with MySQL databases.
Thanks for your time & work on this project.
In the documentation, this is suggested when using the UUID to replace the normal incrementing ID:
$table->uuid('id')->unique();
$table->primary('id');
Would that not create two indexes on the table - a unique index, and a primary key index that also happens to be unique? Just the primary index will be sufficient.
The main difference between the two types of index is that a primary key cannot hold a null but a unique index can (unless you are using MS SQL, which treats a null as a unique value, rather than being ignored, as it should be IMO).
I'm building an API and, as part of testing, I would like to create a resource where the UUID has been generated by the client and sent as part of the create request.
Please can you allow this functionality by not overwriting the UUID if the field is already populated?
Thanks!
I tried installing this package with Laravel 7.0 i got this errror
` composer require emadadly/laravel-uuid
Using version ^1.3 for emadadly/laravel-uuid
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Installation request for emadadly/laravel-uuid ^1.3 -> satisfiable by emadadly/laravel-uuid[v1.3.0].
- Conclusion: remove laravel/framework v7.3.0
- Conclusion: don't install laravel/framework v7.3.0
- emadadly/laravel-uuid v1.3.0 requires illuminate/support 5.2.|5.3.|5.4.|5.5.|5.6.|5.7.|5.8.|6. -> satisfiable by illuminate/support[5.2.x-dev, 5.3.x-dev, 5.4.x-dev, 5.5.x-dev, 5.6.x-dev, 5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, 5.8.x-dev, 6.x-dev, v5.2.0, v5.2.19, v5.2.21, v5.2.24, v5.2.25, v5.2.26, v5.2.27, v5.2.28, v5.2.31, v5.2.32, v5.2.37, v5.2.43, v5.2.45, v5.2.6, v5.2.7, v5.3.0, v5.3.16, v5.3.23, v5.3.4, v5.4.0, v5.4.13, v5.4.17, v5.4.19, v5.4.27, v5.4.36, v5.4.9, v5.5.0, v5.5.16, v5.5.17, v5.5.2, v5.5.28, v5.5.33, v5.5.34, v5.5.35, v5.5.36, v5.5.37, v5.5.39, v5.5.40, v5.5.41, v5.5.43, v5.5.44, v5.6.0, v5.6.1, v5.6.10, v5.6.11, v5.6.12, v5.6.13, v5.6.14, v5.6.15, v5.6.16, v5.6.17, v5.6.19, v5.6.2, v5.6.20, v5.6.21, v5.6.22, v5.6.23, v5.6.24, v5.6.25, v5.6.26, v5.6.27, v5.6.28, v5.6.29, v5.6.3, v5.6.30, v5.6.31, v5.6.32, v5.6.33, v5.6.34, v5.6.35, v5.6.36, v5.6.37, v5.6.38, v5.6.39, v5.6.4, v5.6.5, v5.6.6, v5.6.7, v5.6.8, v5.6.9, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.26, v5.7.27, v5.7.28, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9, v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.17, v5.8.18, v5.8.19, v5.8.2, v5.8.20, v5.8.22, v5.8.24, v5.8.27, v5.8.28, v5.8.29, v5.8.3, v5.8.30, v5.8.31, v5.8.32, v5.8.33, v5.8.34, v5.8.35, v5.8.36, v5.8.4, v5.8.8, v5.8.9, v6.0.0, v6.0.1, v6.0.2, v6.0.3, v6.0.4, v6.1.0, v6.10.0, v6.11.0, v6.12.0, v6.13.0, v6.13.1, v6.14.0, v6.15.0, v6.15.1, v6.16.0, v6.17.0, v6.17.1, v6.18.0, v6.18.1, v6.18.2, v6.18.3, v6.2.0, v6.3.0, v6.4.1, v6.5.0, v6.5.1, v6.5.2, v6.6.0, v6.6.1, v6.6.2, v6.7.0, v6.8.0].
- don't install illuminate/support 5.5.x-dev|don't install laravel/framework v7.3.0
- don't install illuminate/support v5.5.0|don't install laravel/framework v7.3.0
`
Attempting to use this, but when saving no UUID is entered into the db.
Here is my setup:
DB:
Name: uuid char(36) utf8mb4_unicode_ci
Account Model:
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\Sluggable;
use Emadadly\LaravelUuid\Uuids;
class Account extends Model
{
use Uuids;
Controller:
`public function saveUUID(Account $account){
$example = new Account;
$example->name = 'test';
$example->short = 'FIVE';
$example->industry_id = 1;
$example->user_id = 1;
$example->status_id = 36;
$example->save();
return response()->json(['example' => $example]);
}`
The uuid field is left null, and the response for $example does not include the uuid.
What am i missing or doing wrong?
Need update for laravel version 6
I added the package to my laravel 7.20 and the feature didn't work.
The trait wasn't being triggered at all. I had do add to the app.php
manually.
Hello there,
I would recommend adding a record existence check before assigning a uuid to a model instance.
As many users may use this uuid utility as a primary key, it shouldn't break down the functionality when the data store is expanded on a larger scale, where the collision of id with existing record may occur.
- Installation request for emadadly/laravel-uuid ^1.2 -> satisfiable by emadadly/laravel-uuid[1.2.1.x-dev, v1.2, v1.2.1].
- Conclusion: remove laravel/framework v5.6.3
- Conclusion: don't install laravel/framework v5.6.3
Hi, is it possible to support the new PHP 8?
Thank you very much in advance!
When the model has observer on created event, then the UUID is not saved.
if I put it in production the above error appears.
When the model has many to many relations, then the UUID is not saved with attach and sync functions.
Hello ... thanks for the great work! Any chance you plan on adding downcased UUIDs perhaps via a config switch? I had to pull the trait into my own code-base just to make the simple change of removing strtoupper and modifying the regex. I believe downcased UUIDs are considered a best practice? I'm happy to do a PR if you don't have the time or inclination.
I recently install Laravel 5.7 and followed all the steps given in readme.
When running db:seed to add user, throws following error. Upon debugging deeper, looks like UUID is not attached when saving instance of User.
$user = new \App\User();
$user->name = 'Xyz;
$user->email ='[email protected]';
$user->password = bcrypt('secret123456');
$user->save();
namespace App;
use Emadadly\LaravelUuid\Uuids;
use Laravel\Passport\HasApiTokens;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
/**
* Class User
*
* @package App
*/
class User extends Authenticatable
{
use Notifiable, HasApiTokens;
use SoftDeletes;
use Uuids;
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name', 'email', 'password', 'api_token'
];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [
'password', 'remember_token', 'api_token'
];
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
}
Symfony\Component\Debug\Exception\FatalThrowableError : Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed in /home/vagrant/projects/web/laravel-projects/contentz/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 612 and exactly 2 expected at /home/vagrant/projects/web/laravel-projects/contentz/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php:557
Hi,
I believe that pipeline has failed on master with php 8 support. Therefore, the changes for php 8 support aren't released.
Thanks
I'm getting this error:
Symfony\Component\Debug\Exception\FatalThrowableError: Class 'Ramsey\Uuid\Uuid' not found
in /Users/jeremias/Intranet-Bet-el/vendor/emadadly/laravel-uuid/src/Uuids.php:17
Why?
thanks for the great work!
Although you updated the composer.json to support laravel 5.7, Packagist is apparently not aware of it.
Package fails on install on version 5.8.
Do you know when support for 5.8 will be added?
Thanks
Stan
- Installation request for emadadly/laravel-uuid ^1.2 -> satisfiable by emadadly/laravel-uuid[v1.2].
- Conclusion: remove laravel/framework v5.5.0
- Conclusion: don't install laravel/framework v5.5.0
Can you tell me how I might do this. I have a backup database that I want to insert records with id/UUID primary key set using eloquent create() method. Basically, I want to export UUID to new database using same model, without generating new UUID
class User extends Model
{
use Uuids;
public $incrementing = false;
protected $fillable = [
'id',
Hi, all right?
In my Model, using $primaryKey attributes. I can use this attribute to set column name?
Just a suggestion, for improvement of the package.
Laravel uses the https://github.com/ramsey/uuid for generating UUID's: https://github.com/laravel/framework/blob/5.4/composer.json#L28. At least for notifications (https://github.com/laravel/framework/blob/5.4/src/Illuminate/Notifications/NotificationSender.php#L82).
Since ramsey/uuid follows the RFC 4122 specification, that might be better way to generate the UUID's, instead of relying on your own method.
Hi, thanks for the easy to use package!
I had to modify the boot method in one of my models to add global scopes but then I found out that Uuids were no longer being generated, so I solved it like this:
use Uuids {
boot as protected uuid_boot;
}
instead of:
use Uuids;
then in the modified boot method:
protected static function boot()
{
parent::boot();
self::uuid_boot();
// static::addGlobalScope(...);
}
Please add this somewhere in the readme/docs/whatever. Thank you!
Laravel 5.8.38
Just found a small gotcha. If you are using Auth::id()
method to get an authenticated users id it will return 0
. I found out in your models you need to need to cast the id to a string like so:
class User extends Authenticatable
{
use Uuids;
protected $casts = [
'id' => 'string'
];
}
Not really an issue, but a small improvement that I think should be added to the documentation.
Thanks for the library!
There is a commit here to update the package to support Laravel 9 2b7456e, however this update has not be published in a new package version https://packagist.org/packages/emadadly/laravel-uuid
Thanks.
I already have an user table with rows, if I add uuid column, how can I generate uuid to all my rows?
Type error: Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed in /Users/user/Code/y/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php on line 519 and exactly 2 expected
in HasAttributes.php line 511
at Model->setAttribute(array()) in HasAttributes.php line 519
at Model->setAttribute('', array()) in Model.php line 1246
at Model->__set('', array()) in Uuids.php line 22
at Staff::Emadadly\LaravelUuid{closure}(object(Staff)) in Dispatcher.php line 334
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.