├── .gitignore ├── README.md ├── composer.json └── src └── HasUuidTrait.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | vendor -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Eloquent UUID 2 | 3 | [![Packagist](https://img.shields.io/packagist/v/jamesmills/eloquent-uuid.svg?style=for-the-badge)](https://packagist.org/packages/jamesmills/eloquent-uuid) 4 | ![Packagist](https://img.shields.io/packagist/dt/jamesmills/eloquent-uuid.svg?style=for-the-badge) 5 | ![Packagist](https://img.shields.io/packagist/l/jamesmills/eloquent-uuid?style=for-the-badge) 6 | [![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen?style=for-the-badge)](https://plant.treeware.earth/jamesmills/eloquent-uuid) 7 | 8 | A Laravel Eloquent Model trait for adding and using a uuid with models. 9 | 10 | The trait listens to the `creating` event. It generates a new UUID and saves it in the uuid column on the model. 11 | 12 | Featured in [Laravel News](https://laravel-news.com/eloquent-uuid-package-for-laravel) 13 | 14 | ## Installation 15 | 16 | ``` 17 | composer require jamesmills/eloquent-uuid 18 | ``` 19 | 20 | ## Use 21 | 22 | In order to use this in your models, just put `use HasUuidTrait;` 23 | 24 | ```php 25 | primary('id'); 45 | $table->uuid('uuid')->unique(); // this will create a CHAR(36) field 46 | $table->string('username', 32); 47 | $table->string('password', 50); 48 | // ... 49 | }); 50 | ``` 51 | 52 | ## Querying your models 53 | 54 | You may use the `findByUuidOrFail` method to try and fetch a model directly: 55 | 56 | ```php 57 | first(); 83 | if (! $user) { 84 | // Do something else... 85 | } 86 | }); 87 | ``` 88 | ```php 89 | input('uuids'); 94 | 95 | // Try to get the Users 96 | $users = App\User::withUuids($uuids)->all(); 97 | 98 | // Handle the delete and return 99 | $users->delete(); 100 | }); 101 | ``` 102 | 103 | ## Licence 104 | 105 | This package is 100% free and open-source, under the MIT license. Use it however you want. 106 | 107 | This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/jamesmills/eloquent-uuid) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. 108 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jamesmills/eloquent-uuid", 3 | "description": "A Laravel Eloquent Model trait for adding and using a uuid with models", 4 | "type": "package", 5 | "license": "MIT", 6 | "keywords": ["laravel", "eloquent", "uuid"], 7 | "authors": [ 8 | { 9 | "name": "James Mills", 10 | "email": "james@jamesmills.co.uk" 11 | } 12 | ], 13 | "require": { 14 | "php": ">=7.2.0", 15 | "illuminate/support": "5.4.* || 5.5.* || 5.6.*|| 5.7.*|| 5.8.*|| ^6 || ^7 || ^8", 16 | "ramsey/uuid": "^4.1" 17 | }, 18 | "autoload": { 19 | "psr-4": { 20 | "JamesMills\\Uuid\\": "src/" 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /src/HasUuidTrait.php: -------------------------------------------------------------------------------- 1 | uuid) { 14 | $model->uuid = (string)Uuid::uuid4(); 15 | } 16 | }); 17 | } 18 | 19 | public static function findByUuidOrFail($uuid) 20 | { 21 | return self::whereUuid($uuid)->firstOrFail(); 22 | } 23 | 24 | /** 25 | * Eloquent scope to look for a given UUID 26 | * 27 | * @param \Illuminate\Database\Eloquent\Builder $query 28 | * @param String $uuid The UUID to search for 29 | * @return \Illuminate\Database\Eloquent\Builder 30 | */ 31 | public function scopeWithUuid($query, $uuid) 32 | { 33 | return $query->where('uuid', $uuid); 34 | } 35 | 36 | /** 37 | * Eloquent scope to look for multiple given UUIDs 38 | * 39 | * @param \Illuminate\Database\Eloquent\Builder $query 40 | * @param Array $uuids The UUIDs to search for 41 | * @return \Illuminate\Database\Eloquent\Builder 42 | */ 43 | public function scopeWithUuids($query, Array $uuids) 44 | { 45 | return $query->whereIn('uuid', $uuids); 46 | } 47 | 48 | /** 49 | * Get the route key for the model. 50 | * 51 | * @return string 52 | */ 53 | public function getRouteKeyName() 54 | { 55 | return 'uuid'; 56 | } 57 | } 58 | --------------------------------------------------------------------------------