PHP code example of a-sabagh / laravel-sqids

1. Go to this page and download the library: Download a-sabagh/laravel-sqids 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/ */

    

a-sabagh / laravel-sqids example snippets


use ASabagh\LaravelSqids\Facades\Sqids;

$id = 123;

$encodeString = Sqids::encode([$id]); // e.g. "Lqj8a0"

$decodeNumber = Sqids::decode($encodeString); // [123]

$id = 456;

$encodeString = Sqids::encodeInteger($id);

$decodeInteger = Sqids::decodeInteger($encodeString);

$decodeCollection = Sqids::decodeCollect($encodeString);

'drivers' => [
  'default' => [
    'pad' => env('SQIDS_DEFAULT_PAD', ''),
    'length' => env('SQIDS_DEFAULT_LENGTH', 6),
    'blocklist' => env('SQIDS_DEFAULT_BLOCK_LIST', []),
    'alphabet' => env('SQIDS_DEFAULT_ALPHABET', 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'),
  ],
]

'drivers' => [
    'default' => [ /* default config */ ],

    'short_ids' => [
        'pad'       => '0',
        'length'    => 4,
        'blocklist' => ['0', 'O', 'I', '1'],
        'alphabet'  => 'ABCDEFGHJKLMNPQRSTUVWXYZ23456789',
    ],
],

$encoded = Sqids::driver('short_ids')->encode([$id]);
$decoded = Sqids::driver('short_ids')->decode($encoded);

$shortId = Sqids::shortIds()->encode([$id]);
$defaultId = Sqids::default()->encode([$id]);

$isValid = Sqids::encodedStringValid($randomString);

$encoded = Sqids::encodeInteger($id);

$validator = Validator::make(
    ['endpoint' => $encoded],
    ['endpoint' => [new SqidsValidationRule]]
);

if ($validator->passes()) {
    // Valid Sqid
}

// Encode multiple numbers
$encoded = sqids([1, 2, 3]); // e.g. "Lqj8a0"

// Decode back
$decoded = unsqids($encoded); // [1, 2, 3]

// Encode a single integer
$singleEncoded = sqidsInt(123); // e.g. "M7k2b1"

// Decode single integer
$singleDecoded = unsqidsInt($singleEncoded); // 123

// Decode into a Collection
$collection = unsqidsCollect($encoded); // Collection([1, 2, 3])
bash
php artisan vendor:publish --tag=sqids