Install Laravel Hashids via Composer
masterInstall the package in your Laravel project's root directory using Composer.
composer require vinkla/hashidsrepository·master·Indexed 24 days ago
https://github.com/vinkla/laravel-hashidsA Laravel bridge for the Hashids library that allows developers to encode and decode integers into short, unique, non-sequential strings. It provides a Hashids facade, HashidsManager for dependency injection, and support for multiple connections via configuration in config/hashids.php.
Install the package in your Laravel project's root directory using Composer.
composer require vinkla/hashidsTo configure your Hashids settings, publish the vendor assets to your application. This creates a config/hashids.php file where you can define your connections and the default connection.
Key configuration options in config/hashids.php:
default: Specifies which connection to use by default (defaults to main).connections: An array where you define multiple Hashids connections for your application.php artisan vendor:publishTo customize the Hashids settings, you can publish the package's configuration file to your application's config directory. This allows you to manage your salt, length, and alphabet settings within your own codebase.
Run the following Artisan command:
php artisan vendor:publish --provider="Vinkla\Hashids\HashidsServiceProvider"Instead of using Facades, you can inject Vinkla\Hashids\HashidsManager into your class constructors. The manager behaves like a standard Hashids\Hashids class.
use Vinkla\Hashids\HashidsManager;
class Foo
{
protected $hashids;
public function __construct(HashidsManager $hashids)
{
$this->hashids = $hashids;
}
public function bar($id)
{
$this->hashids->encode($id);
}
}
// Resolve and use
App::make('Foo')->bar();The easiest way to use the package is via the Vinkla\Hashids\Facades\Hashids facade. Once configured, you can encode integers into hashids and decode hashids back into integers.
use Vinkla\Hashids\Facades\Hashids;
// Encode integers
Hashids::encode(4815162342);
// Decode strings
Hashids::decode('doyouthinkthatsairyourebreathingnow');The package supports multiple connections. You can switch between connections using the connection() method on the facade. If a connection is set as the default in your config, calling connection() without arguments or calling methods directly on the facade will use that default connection.
Methods for connection management:
connection($name): Selects a specific connection by name.getDefaultConnection(): Returns the name of the current default connection.setDefaultConnection($name): Changes the default connection for the current request.The HashidsManager is the central entry point for managing multiple Hashids configurations. It extends AbstractManager, allowing you to resolve specific Hashids instances by their configuration name. Once an instance is resolved, you can use the following methods provided by the underlying Hashids library:
encode(mixed ...$numbers): Encodes one or more numbers into a hash string.decode(string $hash): Decodes a hash string back into an array of numbers.encodeHex(string $str): Encodes a string into a hex hash.decodeHex(string $hash): Decodes a hex hash back into a string.The package registers several bindings in the Laravel service container. You can resolve these using the following keys:
hashids: Resolves to the HashidsManager. This is the primary entry point for managing multiple hashid connections.hashids.factory: Resolves to the HashidsFactory. Used for creating new Hashids instances.hashids.connection: Resolves to a Hashids instance (the default connection). This is the most common way to access the hashing and decoding functionality directly.Vinkla\Hashids\Facades\Hashids facade provides a static interface to the underlying Hashids service. You can use it to encode numbers into hashes, decode hashes back into numbers, or handle hexadecimal conversions.Hashids\Hashids instance (for example, to access specific instance methods not exposed by the facade), you can use the connection() method. This method allows you to retrieve the service instance, optionally by a specific connection name.