承接 a-sabagh/laravel-sqids 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

a-sabagh/laravel-sqids

最新稳定版本:v1.1.0

Composer 安装命令:

composer require a-sabagh/laravel-sqids

包简介

Laravel integration for sqids-php: Generate short YouTube-looking IDs from numbers

README 文档

README

Packagist License

Laravel adapter for sqids-php.
Generate short, unique, non-sequential IDs for your models, routes, validation, and more.

Table of Contents

Features

  • Generate short, unique, non-sequential IDs
  • Easy helpers: sqid($id) / unsqid($hash)
  • Facade: Sqids::encode() / decode()
  • Validation rule: sqid
  • Fully configurable alphabet, min length, and blocklist

Installation

composer require a-sabagh/laravel-sqids

Simple Encode & Decode

The Sqids service allows you to convert integers into short, URL-safe strings and decode them back.

Important

Sqids require either the bcmath or gmp extension in order to work.

Usage

use ASabagh\LaravelSqids\Facades\Sqids;

$id = 123;

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

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

Encode and Decode Single Integer

$id = 456;

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

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

Decode and get a Laravel Collection

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

Notes:

  • encode always expects an array of integers.

  • decode returns an array.

  • encodeInteger / decodeInteger work with single integers.

  • decodeCollect returns a Collection for easier chaining with Laravel collections.

Configuration

Laravel Sqids allows you to customize its behavior via a configuration file. Publishing the configuration lets you modify settings such as the alphabet, minimum length, and blocklist.

Publish the Config File

Use the following Artisan command to publish the configuration to your application:

php artisan vendor:publish --tag=sqids

Here are each of the drivers setup for your application. Example configuration has been included, but you may add as many drivers as you would like.

'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'),
  ],
]

Laravel Sqids allows you to publish the configuration and define multiple drivers, each with its own settings.

Driver Setup

The configuration file includes a drivers array. You can configure one or more drivers for different encoding strategies.

You can add additional drivers with their own configuration:

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

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

Then, you can use a specific driver when encoding/decoding:

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

Once a driver has been registered and configured in your config/sqids.php, you can access it directly using a camelCase method on the Sqids Facade.

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

Check if an Encoded String is Valid

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

Sqids Custom Validation Rule

Laravel Sqids provides a custom validation rule to ensure that a given input is a valid Sqid string. You can use it in form requests, inline validation, and with custom drivers.

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

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

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

Available Helpers

Function
sqids(array $numbers, ?string $driver = null): string
unsqids(string $encodedString, ?string $driver = null): array
sqidsInt(int $id, ?string $driver = null): int
unsqidsInt(string $encodedString, ?string $driver = null): int
unsqidsCollect(string $encodedString, ?string $driver = null): Collection
// 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])

统计信息

  • 总下载量: 190
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 5
  • 点击次数: 5
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固