yieldstudio / eloquent-public-id
Eloquent Public ID Trait for Laravel 9 and above.
Installs: 39 993
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:plugin
Requires
- php: ^8.0|^8.1|^8.2
- illuminate/database: ^9|^10.0|^11.0
- illuminate/support: ^9|^10.0|^11.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- laravel/pint: ^1.16
- orchestra/testbench: ^7.0|^8.0|^9.0.2
- pestphp/pest: ^1.21|^2.0
- phpunit/phpunit: ^9.4|^10.5|^11.0
README
What It Does
The interest of public IDs is to keep a whole and incremental ID, while having a UUID to expose to the front end, which can be convenient for security reasons.
This package offers two features:
- Allow models to manage a public ID
- Allow FormRequest to easily convert public IDs to IDs
Installation
You can install the package via composer:
composer require yieldstudio/eloquent-public-id
HasPublicId Trait
This Trait will enable your Model to have benefit of all the actions needed to process the public id.
Once package installed, Add a public id field into your table
Schema::create('users', function (Blueprint $table) { // .. $table->uuid('public_id')->index()->unique(); // .. });
Next step, use the HasPublicId trait into your Model
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use YieldStudio\EloquentPublicId\HasPublicId; class User extends Model { use HasPublicId; }
It's ready to work :)
⚠️ By default the trait will mark the ID field as a hidden field and guard the public ID.
The Trait adds some methods to your Model, here they are:
Change the name of the public ID column
If in your migration you have chosen another field name instead of public_id, you have to specify this field using the getPublicIdName
function.
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use YieldStudio\EloquentPublicId\HasPublicId; class User extends Model { use HasPublicId; public function getPublicIdName(): string { return 'uuid'; } }
Change the generation of the public ID
The public id is automatically generated once your Model is created in the database.
If you want to modify the value of the generation of this field, you must add the generatePublicId
function to your Model
<?php declare(strict_types=1); namespace App\Models; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Str; use YieldStudio\EloquentPublicId\HasPublicId; class User extends Model { use HasPublicId; public function generatePublicId(): string { return Str::random(); } }
ConvertPublicId Trait
Allowing to convert public IDs to IDs in a FormRequest (before validation).
<?php declare(strict_types=1); namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use YieldStudio\EloquentPublicId\ConvertPublicId; class CreatePostRequest extends FormRequest { use ConvertPublicId; protected array $publicIdsToConvert = [ 'category_id' => Category::class, 'tags.*' => Tag::class, 'postable_id' => 'postable_type', // You can reference another field as model class in case of morph relationship 'suggestions' => [ // Nesting fields is allowed '*' => [ 'post_id' => Post::class, 'tags.*' => Tag::class, 'postable_id' => 'postable_type', ] ] ];
Unit tests
To run the tests, just run composer install
and composer test
.
Contact us
Our team at Yield Studio is ready to welcome you and make every interaction an exceptional experience. You can contact us.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail contact@yieldstudio.fr instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.