├── CHANGELOG.md ├── LICENSE.md ├── README.md ├── composer.json └── src └── Traits ├── HasUUID.php └── UUIDIsPrimaryKey.php /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | All notable changes to this project will be documented in this file. 3 | 4 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 5 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 6 | 7 | ## [1.0.2] - 2019-05-07 8 | - Added additional testing and doc files 9 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Binary Cabin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel-UUID 2 | 3 | [![Build Status](https://travis-ci.org/binarycabin/laravel-uuid.svg?branch=master)](https://travis-ci.org/binarycabin/laravel-uuid) 4 | [![StyleCI](https://github.styleci.io/repos/110678597/shield?branch=master)](https://github.styleci.io/repos/110678597) 5 | [![Latest Stable Version](http://img.shields.io/packagist/v/binarycabin/laravel-uuid.svg?style=flat)](https://packagist.org/packages/binarycabin/laravel-uuid) 6 | [![Total Downloads](http://img.shields.io/packagist/dt/binarycabin/laravel-uuid.svg?style=flat)](https://packagist.org/packages/binarycabin/laravel-uuid) 7 | 8 | A wrapper for webpatser/laravel-uuid with additional integration 9 | 10 | ```bash 11 | composer require binarycabin/laravel-uuid 12 | ``` 13 | 14 | This package adds a very simple trait to automatically generate a UUID for your Models. 15 | 16 | Simply add the "\BinaryCabin\LaravelUUID\Traits\HasUUID;" trait to your model: 17 | 18 | ```php 19 | first(); 44 | ``` 45 | 46 | And static find method: 47 | 48 | ```php 49 | \App\Project::findByUUID('uuid') 50 | ``` 51 | 52 | A second trait is available if you use your UUIDs as primary keys: 53 | 54 | ```php 55 | 5.0" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/Traits/HasUUID.php: -------------------------------------------------------------------------------- 1 | getUUIDFieldName(); 11 | if (empty($model->$uuidFieldName)) { 12 | $model->$uuidFieldName = static::generateUUID(); 13 | } 14 | }); 15 | } 16 | 17 | public function getUUIDFieldName() 18 | { 19 | if (! empty($this->uuidFieldName)) { 20 | return $this->uuidFieldName; 21 | } 22 | 23 | return 'uuid'; 24 | } 25 | 26 | public static function generateUUID() 27 | { 28 | return \Uuid::generate()->string; 29 | } 30 | 31 | public function scopeByUUID($query, $uuid) 32 | { 33 | return $query->where($this->getUUIDFieldName(), $uuid); 34 | } 35 | 36 | public static function findByUuid($uuid) 37 | { 38 | return static::byUUID($uuid)->first(); 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/Traits/UUIDIsPrimaryKey.php: -------------------------------------------------------------------------------- 1 |