Download the PHP package hadi-aghandeh/laravel-friendly-id without Composer
On this page you can find all versions of the php package hadi-aghandeh/laravel-friendly-id. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hadi-aghandeh/laravel-friendly-id
More information about hadi-aghandeh/laravel-friendly-id
Files in hadi-aghandeh/laravel-friendly-id
Package laravel-friendly-id
Short Description map integer id to a friendly string
License MIT
Informations about the package laravel-friendly-id
Laravel Friendly ID - Laravel String ID
Looking to convert the integer primary ID of your laravel model to an user friendly string?
This package enables you to generate string-based IDs for your models, without the need for a separate ID column. By default, Laravel uses integer IDs with MySQL, but in many cases, you may need unique string identifiers for your models. This package helps you implement these unique string IDs seamlessly.
1335684976 -> encode -> xxx-xxxx-xxx -> decode -> 1335684976
Here are some potential uses for having a string representation of IDs:
- URL Creation: A string representation of integer IDs can enhance your application's readability by masking real IDs and improving clarity, especially in URLs. Such as Google Meet style urls
- Model Tracking: You can use string IDs instead of integers for better user experience.
- Reference Codes: For customer-facing references, like invoices or orders, string IDs are more user-friendly than raw numbers.
- API Responses: String-based IDs can make API data less predictable and easier to manage across different systems.
- Cross-System Compatibility: Some external systems may require a string format for identifiers, and strings can offer better flexibility for integration.
Laravel Friendly ID is easy to use:
Installation
Install the package via Composer:
Configuration
After installing the package, publish the configuration file:
This will create a config/friendly-id.php
file where you can customize the encoding settings.
Default Configuration
alphabet
: The set of characters used to encode the ID. By default, it uses"abcdefghijklmnopqrstu"
. You can modify it via environment variables.encoder
: The encoder algorithm used to create friendly IDs. The default encoder is"SQIDS"
. You can change this through theFRIENDLY_ID_ENCODER
environment variable. available encoders areBASEN
andSQIDS
Encoders
BASEN
This changes the base of the integer to letters provided by FRIENDLY_ID_ALPHABET.
Example output with a length of 7:
SQIDS
This uses the well-known Sqids algorithm with its PHP package.
Example output with a length of 10:
WORDS
Sometimes, you may prefer your string to be a mix of words. These strings are easier to remember due to their use of vowels.
Usage
To start using friendly IDs in your Laravel models, simply use the FriendlyId
trait in your model class.
Example
Add Trait to a Model
Encode and Decode Friendly IDs
You can now use the encodeFriendlyId
method to generate a friendly string representation of the model's ID:
To decode a friendly ID back to the original integer ID, use the decodeFriendlyId
method:
Query by Friendly ID
You can use the whereFriendlyId
scope to query a model by its friendly ID:
Configuration Customization
To customize the encoding behavior, you can update your .env
file with the following environment variables:
FRIENDLY_ID_ALPHABET
: Defines the custom alphabet used to generate friendly IDs.FRIENDLY_ID_ENCODER
: Defines the encoding mechanism to use. Currently, the default is"SQIDS"
.
Exception Handling
The decodeFriendlyId
method will return null
if the provided friendly ID is invalid or cannot be decoded.
Testing
You can run tests for this package using PHPUnit:
License
This package is open-sourced software licensed under the MIT license.
Support
If this package helps you, please consider giving it a ⭐ on GitHub! Your support means a lot and helps to maintain this project.