1. Go to this page and download the library: Download litepie/hashids library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
// Encode an ID
$hash = hashids_encode(123); // returns something like "bM"
// Decode a hash
$id = hashids_decode('bM'); // returns 123
// Encode multiple values
$hash = hashids_encode([123, 456]); // encode multiple values
// Decode to array
$ids = hashids_decode('86Rf07'); // returns [123, 456]
// Using new Sqids functions (aliases)
$hash = sqids_encode(123); // same as hashids_encode
$id = sqids_decode('bM'); // same as hashids_decode
use Litepie\Hashids\Facades\Hashids;
$hash = Hashids::encode([123]);
$id = Hashids::decode('bM');
use Litepie\Hashids\Traits\Hashids;
class User extends Model
{
use Hashids;
// Your model code...
}
// Get encoded ID
$user = User::find(1);
echo $user->eid; // encoded ID attribute
echo $user->getRouteKey(); // for route model binding
// Find by encoded ID
$user = User::findOrFail('bM');
$user = User::findOrNew('bM');
// Signed IDs (with expiration)
$signedId = $user->getSignedId('+1 hour'); // expires in 1 hour
$signedId = $user->getSignedId(1234567890); // expires at timestamp
$signedId = $user->getSignedId(); // never expires
// Find by signed ID
$user = User::findBySignedId($signedId);
// routes/web.php
Route::get('/users/{user}', function (User $user) {
return $user;
});