Download the PHP package rossbearman/eloquent-calamari without Composer
On this page you can find all versions of the php package rossbearman/eloquent-calamari. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download rossbearman/eloquent-calamari
More information about rossbearman/eloquent-calamari
Files in rossbearman/eloquent-calamari
Package eloquent-calamari
Short Description Obfuscate incrementing IDs with Sqids for Laravel and Eloquent.
License MIT
Homepage https://github.com/rossbearman/eloquent-calamari
Informations about the package eloquent-calamari
Eloquent Calamari - Sqids for Laravel Models
Eloquent Calamari integrates the Sqids[^1] algorithm into Laravel and Eloquent, enabling you to seamlessly use obfuscated, unique IDs in place of your internal auto-incrementing IDs.
- Obfuscate auto-incrementing IDs with short, unique identifiers
- Unique Sqids across models, ID 1 will be represented differently on every model
- Optional route model binding with
SqidBasedRouting
- Transparently handle non-canonical IDs
example.com/link/2fC37YMkO
=== Link::find(1)
example.com/video/TaRfL1RAK
=== Video::find(1)
Getting Started
Require this package with Composer.
composer require rossbearman/eloquent-calamari
Add the HasSqid
and SqidBasedRouting
traits to your models.
Create a route for your model.
example.com/customer/3irWXI2rFV
now returns the Customer details.
Querying
Common query methods are also available.
Representation
By default the Sqid is not included in the model's toArray()
or toJson()
output and the ID is not hidden from these. You can use Eloquent's appends
and hidden
properties to achieve this.
Custom Routing
You can take advantage of SqidBasedRouting
while still having a different default binding by overriding the model's getRouteKeyName()
method.
Configuration
By default, Eloquent Calamari generates a random alphabet for each model, by shuffling the default alphabet with the Xoshiro256StarStar algorithm, seeded with a combination of the name of the model and the key set in the config.
This ensures that entities of different models with the same ID will have a unique Sqid, however it is fragile to the model name or app key being changed. If either of these are changed, Sqids will no longer resolve back to the same ID.
[!IMPORTANT] It is highly recommended that you explicitly set a pre-shuffled alphabet for each model using the
sqids.alphabets
config key, which will disable the shuffling behaviour for that model.
Setting alphabets
Start by publishing the sqids.php
config file to your config
directory.
Then generate a new alphabet for your model.
You can also generate alphabets for multiple models at once.
Follow the instructions provided by the command to add the new keys to your config/sqids.php
and .env
files.
Confirm alphabet is being used
To ensure that Eloquent Calamari is using the expected alphabet for a specific model, use the following command.
This will list all the models that have successfully been registered in the config and whether the class string can be resolved to a class in your application.
Setting minimum Sqid lengths
By default, all Sqids will be a minimum of 10 characters. You can adjust this for each model by assigning different values (to a minimum of 3) to the sqids.min_lengths
config array.
The maximum length of a Sqid is dependent on the input ID and the alphabet used. A more varied alphabet (upper and lower case letters, numbers and symbols) will result in shorter Sqids.
Canonical Sqids
By design, multiple Sqids can resolve to the same number, however Eloquent Calamari will always return the same Sqid for a given number. Furthermore, this is the only Sqid that can be used to access an entity, and any other Sqid that would normally resolve to the same number will be rejected.
This check can be disabled on a per-model basis by adding an entry to the sqids.canonical_checks
config array.
Development
PHPUnit tests:
PHPStan analysis:
Pint linting:
Security
Please email Ross Bearman [email protected] if you have discovered a vulnerability in this package.
License
MIT License (MIT). Please see LICENSE.md for more information.
[^1]: Sqids is the latest version of the Hashids algorithm, redesigned to accomodate custom blocklists and a better encoding scheme.
All versions of eloquent-calamari with dependencies
ext-mbstring Version *
illuminate/config Version ^10.0 | ^11.0
illuminate/contracts Version ^10.0 | ^11.0
illuminate/database Version ^10.0 | ^11.0
illuminate/support Version ^10.0 | ^11.0
sqids/sqids Version ^0.4.0