├── .github └── workflows │ └── tests.yml ├── .gitignore ├── .php_cs ├── CONTRIBUTING.md ├── LICENSE.md ├── README.md ├── composer.json ├── media ├── logo.afdesign ├── logo.svg └── repository-open-graph.png ├── phpunit.xml ├── src ├── Console │ ├── Stubs │ │ └── model.stub │ └── UuidModelCommand.php ├── Database │ └── Eloquent │ │ ├── Model.php │ │ └── Uuid.php ├── Foundation │ └── Auth │ │ └── User.php └── UuidServiceProvider.php └── tests ├── Database └── Eloquent │ ├── ModelExtendingClassWithUuidTest.php │ ├── ModelExtendingClassWithoutUuidTest.php │ ├── ModelUsingTraitWithUuidTest.php │ └── ModelUsingTraitWithoutUuidTest.php ├── Models ├── TestModelExtendingClassWithUuid1.php ├── TestModelExtendingClassWithUuid4.php ├── TestModelExtendingClassWithoutUuid1.php ├── TestModelExtendingClassWithoutUuid4.php ├── TestModelUsingTraitWithUuid1.php ├── TestModelUsingTraitWithUuid4.php ├── TestModelUsingTraitWithoutUuid1.php └── TestModelUsingTraitWithoutUuid4.php └── TestCase.php /.github/workflows/tests.yml: -------------------------------------------------------------------------------- 1 | name: Tests 2 | 3 | on: 4 | - push 5 | - pull_request 6 | 7 | jobs: 8 | build_and_test: 9 | name: Build and test 10 | 11 | runs-on: ubuntu-20.04 12 | 13 | strategy: 14 | matrix: 15 | php: ["8.1", "8.2"] 16 | 17 | steps: 18 | - name: Setup PHP 19 | uses: shivammathur/setup-php@v2 20 | with: 21 | php-version: ${{ matrix.php }} 22 | 23 | - name: Checkout current commit 24 | uses: actions/checkout@v2.3.4 25 | 26 | - name: Update composer 27 | run: composer self-update 28 | 29 | - name: Install composer dependencies 30 | run: composer install --no-interaction 31 | 32 | - name: Test 33 | run: composer test 34 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.idea 2 | /.php_cs.cache 3 | /.phpunit.result.cache 4 | 5 | # Created by https://www.gitignore.io/api/macos,phpstorm,composer,visualstudiocode 6 | # Edit at https://www.gitignore.io/?templates=macos,phpstorm,composer,visualstudiocode 7 | 8 | ### Composer ### 9 | composer.phar 10 | /vendor/ 11 | 12 | # Commit your application's lock file https://getcomposer.org/doc/01-basic-usage.md#commit-your-composer-lock-file-to-version-control 13 | # You may choose to ignore a library lock file http://getcomposer.org/doc/02-libraries.md#lock-file 14 | composer.lock 15 | 16 | ### macOS ### 17 | # General 18 | .DS_Store 19 | .AppleDouble 20 | .LSOverride 21 | 22 | # Icon must end with two \r 23 | Icon 24 | 25 | # Thumbnails 26 | ._* 27 | 28 | # Files that might appear in the root of a volume 29 | .DocumentRevisions-V100 30 | .fseventsd 31 | .Spotlight-V100 32 | .TemporaryItems 33 | .Trashes 34 | .VolumeIcon.icns 35 | .com.apple.timemachine.donotpresent 36 | 37 | # Directories potentially created on remote AFP share 38 | .AppleDB 39 | .AppleDesktop 40 | Network Trash Folder 41 | Temporary Items 42 | .apdisk 43 | 44 | ### PhpStorm ### 45 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and WebStorm 46 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 47 | 48 | # User-specific stuff 49 | .idea/**/workspace.xml 50 | .idea/**/tasks.xml 51 | .idea/**/usage.statistics.xml 52 | .idea/**/dictionaries 53 | .idea/**/shelf 54 | 55 | # Generated files 56 | .idea/**/contentModel.xml 57 | 58 | # Sensitive or high-churn files 59 | .idea/**/dataSources/ 60 | .idea/**/dataSources.ids 61 | .idea/**/dataSources.local.xml 62 | .idea/**/sqlDataSources.xml 63 | .idea/**/dynamic.xml 64 | .idea/**/uiDesigner.xml 65 | .idea/**/dbnavigator.xml 66 | 67 | # Gradle 68 | .idea/**/gradle.xml 69 | .idea/**/libraries 70 | 71 | # Gradle and Maven with auto-import 72 | # When using Gradle or Maven with auto-import, you should exclude module files, 73 | # since they will be recreated, and may cause churn. Uncomment if using 74 | # auto-import. 75 | # .idea/modules.xml 76 | # .idea/*.iml 77 | # .idea/modules 78 | 79 | # CMake 80 | cmake-build-*/ 81 | 82 | # Mongo Explorer plugin 83 | .idea/**/mongoSettings.xml 84 | 85 | # File-based project format 86 | *.iws 87 | 88 | # IntelliJ 89 | out/ 90 | 91 | # mpeltonen/sbt-idea plugin 92 | .idea_modules/ 93 | 94 | # JIRA plugin 95 | atlassian-ide-plugin.xml 96 | 97 | # Cursive Clojure plugin 98 | .idea/replstate.xml 99 | 100 | # Crashlytics plugin (for Android Studio and IntelliJ) 101 | com_crashlytics_export_strings.xml 102 | crashlytics.properties 103 | crashlytics-build.properties 104 | fabric.properties 105 | 106 | # Editor-based Rest Client 107 | .idea/httpRequests 108 | 109 | # Android studio 3.1+ serialized cache file 110 | .idea/caches/build_file_checksums.ser 111 | 112 | # JetBrains templates 113 | **___jb_tmp___ 114 | 115 | ### PhpStorm Patch ### 116 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 117 | 118 | # *.iml 119 | # modules.xml 120 | # .idea/misc.xml 121 | # *.ipr 122 | 123 | # Sonarlint plugin 124 | .idea/sonarlint 125 | 126 | ### VisualStudioCode ### 127 | .vscode/* 128 | !.vscode/settings.json 129 | !.vscode/tasks.json 130 | !.vscode/launch.json 131 | !.vscode/extensions.json 132 | 133 | ### VisualStudioCode Patch ### 134 | # Ignore all local history of files 135 | .history 136 | 137 | # End of https://www.gitignore.io/api/macos,phpstorm,composer,visualstudiocode 138 | -------------------------------------------------------------------------------- /.php_cs: -------------------------------------------------------------------------------- 1 | in(__DIR__) 10 | ->exclude([ 11 | 'media', 12 | 'tests', 13 | 'vendor', 14 | ]); 15 | 16 | $rules = [ 17 | '@PSR1' => true, 18 | '@PSR2' => true, 19 | 20 | 'align_multiline_comment' => true, 21 | 'array_indentation' => true, 22 | 'array_syntax' => [ 23 | 'syntax' => 'short', 24 | ], 25 | 'blank_line_after_opening_tag' => true, 26 | 'blank_line_before_statement' => [ 27 | 'statements' => ['return', 'throw'], 28 | ], 29 | 'cast_spaces' => [ 30 | 'space' => 'none', 31 | ], 32 | 'class_attributes_separation' => [ 33 | 'elements' => ['method', 'property'], 34 | ], 35 | 'combine_consecutive_issets' => true, 36 | 'combine_consecutive_unsets' => true, 37 | 'compact_nullable_typehint' => true, 38 | 'concat_space' => [ 39 | 'spacing' => 'one', 40 | ], 41 | 'declare_equal_normalize' => true, 42 | 'declare_strict_types' => true, 43 | 'ereg_to_preg' => true, 44 | 'fully_qualified_strict_types' => true, 45 | 'function_to_constant' => true, 46 | 'function_typehint_space' => true, 47 | 'heredoc_indentation' => true, 48 | 'heredoc_to_nowdoc' => true, 49 | 'list_syntax' => true, 50 | 'logical_operators' => true, 51 | 'lowercase_cast' => true, 52 | 'lowercase_static_reference' => true, 53 | 'magic_constant_casing' => true, 54 | 'magic_method_casing' => true, 55 | 'mb_str_functions' => true, 56 | 'method_chaining_indentation' => true, 57 | 'modernize_types_casting' => true, 58 | 'multiline_comment_opening_closing' => true, 59 | 'multiline_whitespace_before_semicolons' => true, 60 | 'native_function_casing' => true, 61 | 'native_function_type_declaration_casing' => true, 62 | 'new_with_braces' => true, 63 | 'no_alias_functions' => true, 64 | 'no_alternative_syntax' => true, 65 | 'no_blank_lines_after_class_opening' => true, 66 | 'no_blank_lines_after_phpdoc' => true, 67 | 'no_break_comment' => [ 68 | 'comment_text' => 'No break.', 69 | ], 70 | 'no_empty_phpdoc' => true, 71 | 'no_empty_statement' => true, 72 | 'no_extra_blank_lines' => true, 73 | 'no_leading_import_slash' => true, 74 | 'no_leading_namespace_whitespace' => true, 75 | 'no_mixed_echo_print' => true, 76 | 'no_multiline_whitespace_around_double_arrow' => true, 77 | 'no_php4_constructor' => true, 78 | 'no_short_bool_cast' => true, 79 | 'no_short_echo_tag' => true, 80 | 'no_singleline_whitespace_before_semicolons' => true, 81 | 'no_spaces_around_offset' => true, 82 | 'no_unused_imports' => true, 83 | 'no_whitespace_before_comma_in_array' => true, 84 | 'no_whitespace_in_blank_line' => true, 85 | 'normalize_index_brace' => true, 86 | 'object_operator_without_whitespace' => true, 87 | 'ordered_imports' => true, 88 | 'phpdoc_add_missing_param_annotation' => true, 89 | 'phpdoc_align' => [ 90 | 'align' => 'left', 91 | ], 92 | 'phpdoc_annotation_without_dot' => true, 93 | 'phpdoc_indent' => true, 94 | 'phpdoc_no_empty_return' => true, 95 | 'phpdoc_no_package' => true, 96 | 'phpdoc_order' => true, 97 | 'phpdoc_scalar' => true, 98 | 'phpdoc_single_line_var_spacing' => true, 99 | 'phpdoc_summary' => true, 100 | 'phpdoc_trim' => true, 101 | 'phpdoc_trim_consecutive_blank_line_separation' => true, 102 | 'phpdoc_types' => true, 103 | 'phpdoc_types_order' => [ 104 | 'null_adjustment' => 'always_last', 105 | 'sort_algorithm' => 'none', 106 | ], 107 | 'phpdoc_var_annotation_correct_order' => true, 108 | 'phpdoc_var_without_name' => true, 109 | 'random_api_migration' => true, 110 | 'return_type_declaration' => true, 111 | 'semicolon_after_instruction' => true, 112 | 'set_type_to_cast' => true, 113 | 'short_scalar_cast' => true, 114 | 'simple_to_complex_string_variable' => true, 115 | 'simplified_null_return' => true, 116 | 'single_blank_line_before_namespace' => true, 117 | 'single_line_comment_style' => true, 118 | 'single_quote' => true, 119 | 'single_trait_insert_per_statement' => true, 120 | 'standardize_not_equals' => true, 121 | 'ternary_operator_spaces' => true, 122 | 'ternary_to_null_coalescing' => true, 123 | 'trailing_comma_in_multiline_array' => true, 124 | 'trim_array_spaces' => true, 125 | 'unary_operator_spaces' => true, 126 | ]; 127 | 128 | return Config::create() 129 | ->setRules($rules) 130 | ->setFinder($finder); 131 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing 2 | 3 | When contributing to this repository, please first discuss the change you wish 4 | to make via issue, email, or any other method with the owners of this repository 5 | before making a change. 6 | 7 | Please note we have a code of conduct, please follow it in all your interactions 8 | with the project. 9 | 10 | ## Pull Request Process 11 | 12 | 1. Ensure any install or build dependencies are removed and untracked. 13 | 2. Update the `README.md` with details of changes to the interface along with an 14 | example or two. 15 | 3. State whether or not the Pull Request is backwards compatible and if not, 16 | then state what the breaking changes are so that they can be documented in 17 | the release notes. 18 | 4. Create the Pull Request and a reviewer will either accept and merge the Pull 19 | Request, then tag the new release, or alternatively the reviewer will request 20 | for more information regarding the Pull Request or even some changes for you 21 | to make before we can accept the Pull Request. 22 | 23 | ## Code of Conduct 24 | 25 | ### Our Pledge 26 | 27 | In the interest of fostering an open and welcoming environment, we as 28 | contributors and maintainers pledge to making participation in our project and 29 | our community a harassment-free experience for everyone, regardless of age, body 30 | size, disability, ethnicity, gender identity and expression, level of experience, 31 | nationality, personal appearance, race, religion, or sexual identity and 32 | orientation. 33 | 34 | ### Our Standards 35 | 36 | Examples of behavior that contributes to creating a positive environment 37 | include: 38 | 39 | * Using welcoming and inclusive language 40 | * Being respectful of differing viewpoints and experiences 41 | * Gracefully accepting constructive criticism 42 | * Focusing on what is best for the community 43 | * Showing empathy towards other community members 44 | 45 | Examples of unacceptable behavior by participants include: 46 | 47 | * The use of sexualized language or imagery and unwelcome sexual attention or 48 | advances 49 | * Trolling, insulting/derogatory comments, and personal or political attacks 50 | * Public or private harassment 51 | * Publishing others' private information, such as a physical or electronic 52 | address, without explicit permission 53 | * Other conduct which could reasonably be considered inappropriate in a 54 | professional setting 55 | 56 | ### Our Responsibilities 57 | 58 | Project maintainers are responsible for clarifying the standards of acceptable 59 | behavior and are expected to take appropriate and fair corrective action in 60 | response to any instances of unacceptable behavior. 61 | 62 | Project maintainers have the right and responsibility to remove, edit, or 63 | reject comments, commits, code, wiki edits, issues, and other contributions 64 | that are not aligned to this Code of Conduct, or to ban temporarily or 65 | permanently any contributor for other behaviors that they deem inappropriate, 66 | threatening, offensive, or harmful. 67 | 68 | ### Scope 69 | 70 | This Code of Conduct applies both within project spaces and in public spaces 71 | when an individual is representing the project or its community. Examples of 72 | representing a project or community include using an official project e-mail 73 | address, posting via an official social media account, or acting as an appointed 74 | representative at an online or offline event. Representation of a project may be 75 | further defined and clarified by project maintainers. 76 | 77 | ### Enforcement 78 | 79 | Instances of abusive, harassing, or otherwise unacceptable behavior may be 80 | reported by contacting the project team at [hello@goldspecdigital.com](mailto:hello@goldspecdigital.com). 81 | All complaints will be reviewed and investigated and will result in a response 82 | that is deemed necessary and appropriate to the circumstances. The project team 83 | is obligated to maintain confidentiality with regard to the reporter of an 84 | incident. Further details of specific enforcement policies may be posted 85 | separately. 86 | 87 | Project maintainers who do not follow or enforce the Code of Conduct in good 88 | faith may face temporary or permanent repercussions as determined by other 89 | members of the project's leadership. 90 | 91 | ### Attribution 92 | 93 | This Code of Conduct is adapted from the [Contributor Covenant][homepage], 94 | version 1.4, available at [http://contributor-covenant.org/version/1/4][version]. 95 | 96 | [homepage]: http://contributor-covenant.org 97 | [version]: http://contributor-covenant.org/version/1/4/ 98 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2019 - Present GoldSpec Digital Ltd 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 19 | SOFTWARE. 20 | 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |
7 | 8 | 14 | 15 | 37 | 38 | ## Introduction 39 | 40 | A simple drop-in solution for providing UUID support for the IDs of your 41 | Eloquent models. 42 | 43 | Both `v1` and `v4` IDs are supported out of the box, however should you need 44 | `v3` or `v5` support, you can easily add this in. 45 | 46 | ## Installing 47 | 48 | Reference the table below for the correct version to use in conjunction with the 49 | version of Laravel you have installed: 50 | 51 | | Laravel | This package | 52 | |----------|--------------| 53 | | `v5.8.*` | `v1.*` | 54 | | `v6.*` | `v6.*` | 55 | | `v7.*` | `v7.*` | 56 | | `v8.*` | `v8.*` | 57 | | `v9.*` | `v9.*` | 58 | | `v10.*` | `v10.*` | 59 | 60 | You can install the package via composer: 61 | 62 | ```bash 63 | composer require goldspecdigital/laravel-eloquent-uuid:^10.0 64 | ``` 65 | 66 | ## Usage 67 | 68 | There are two ways to use this package: 69 | 1. By extending the provided model classes (preferred and simplest method). 70 | 2. By using the provided model trait (allows for extending another model class). 71 | 72 | ### Extending model 73 | 74 | When creating a Eloquent model, instead of extending the standard Laravel model 75 | class, extend from the model class provided by this package: 76 | 77 | ```php 78 | id; // abb034ae-fcdc-4200-8094-582b60a4281f 165 | 166 | // UUID explicity provided. 167 | $model = Model::create(['id' => '04d7f995-ef33-4870-a214-4e21c51ff76e']); 168 | echo $model->id; // 04d7f995-ef33-4870-a214-4e21c51ff76e 169 | ``` 170 | 171 | ### Specifying UUID versions 172 | 173 | By default, `v4` UUIDs will be used for your models. However, you can also 174 | specify `v1` UUIDs to be used by setting the following property/method on your 175 | model: 176 | 177 | #### When extending the class 178 | 179 | ```php 180 | toString(); 246 | } 247 | } 248 | ``` 249 | 250 | ### Creating models 251 | 252 | In addition of the `make:model` artisan command, you will now have access to 253 | `uuid:make:model` which has all the functionality of the standard `make:model` 254 | command (with exception of not being able to create a pivot model): 255 | 256 | ```bash 257 | php artisan uuid:make:model Models/Post --all 258 | ``` 259 | 260 | ### Database migrations 261 | 262 | The default primary ID column used in migrations will not work with UUID primary 263 | keys, as the default column type is an unsigned integer. UUIDs are 36 character 264 | strings so we must specify this in our migrations: 265 | 266 | ```php 267 | uuid('id')->primary(); 283 | }); 284 | } 285 | } 286 | 287 | class CreatePostsTable extends Migration 288 | { 289 | /** 290 | * Run the migrations. 291 | */ 292 | public function up(): void 293 | { 294 | Schema::create('posts', function (Blueprint $table): void { 295 | // Primary key. 296 | $table->uuid('id')->primary(); 297 | 298 | // Foreign key. 299 | $table->uuid('user_id'); 300 | $table->foreign('user_id')->references('id')->on('users'); 301 | }); 302 | } 303 | } 304 | ``` 305 | 306 | ## Running the tests 307 | 308 | To run the test suite you can use the following command: 309 | 310 | ```bash 311 | composer test 312 | ``` 313 | 314 | ## Contributing 315 | 316 | Please read [CONTRIBUTING.md](CONTRIBUTING.md) for details on our code of 317 | conduct, and the process for submitting pull requests to us. 318 | 319 | ## Versioning 320 | 321 | We use [SemVer](http://semver.org/) for versioning. For the versions available, 322 | see the [tags on this repository](https://github.com/goldspecdigital/laravel-eloquent-uuid/tags). 323 | 324 | ## Authors 325 | 326 | * [GoldSpec Digital](https://github.com/goldspecdigital) 327 | 328 | See also the list of [contributors](https://github.com/goldspecdigital/laravel-eloquent-uuid/contributors) 329 | who participated in this project. 330 | 331 | ## License 332 | 333 | This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) 334 | file for details. 335 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "goldspecdigital/laravel-eloquent-uuid", 3 | "description": "A simple drop-in solution for providing UUID support for the IDs of your Eloquent models.", 4 | "type": "library", 5 | "license": "MIT", 6 | "keywords": [ 7 | "php", 8 | "laravel", 9 | "eloquent", 10 | "uuid" 11 | ], 12 | "homepage": "https://github.com/goldspecdigital/laravel-eloquent-uuid", 13 | "authors": [ 14 | { 15 | "name": "Matthew Inamdar", 16 | "email": "matt@goldspecdigital.com" 17 | } 18 | ], 19 | "autoload": { 20 | "psr-4": { 21 | "GoldSpecDigital\\LaravelEloquentUUID\\": "src/" 22 | } 23 | }, 24 | "autoload-dev": { 25 | "psr-4": { 26 | "GoldSpecDigital\\LaravelEloquentUUID\\Tests\\": "tests/" 27 | } 28 | }, 29 | "require": { 30 | "php": "^8.1", 31 | "laravel/framework": "^10.0" 32 | }, 33 | "require-dev": { 34 | "ext-pdo": "*", 35 | "orchestra/testbench": "^8.0", 36 | "phpunit/phpunit": "^10.0" 37 | }, 38 | "config": { 39 | "sort-packages": true 40 | }, 41 | "scripts": { 42 | "test": "phpunit" 43 | }, 44 | "extra": { 45 | "laravel": { 46 | "providers": [ 47 | "GoldSpecDigital\\LaravelEloquentUUID\\UuidServiceProvider" 48 | ] 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /media/logo.afdesign: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/goldspecdigital/laravel-eloquent-uuid/981a144428dbe948b0e8f8cb31eb4571e0ab4400/media/logo.afdesign -------------------------------------------------------------------------------- /media/logo.svg: -------------------------------------------------------------------------------- 1 | 2 | 3 | 134 | -------------------------------------------------------------------------------- /media/repository-open-graph.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/goldspecdigital/laravel-eloquent-uuid/981a144428dbe948b0e8f8cb31eb4571e0ab4400/media/repository-open-graph.png -------------------------------------------------------------------------------- /phpunit.xml: -------------------------------------------------------------------------------- 1 | 2 |