istiak-tridip/laravel-sqids
Composer 安装命令:
composer require istiak-tridip/laravel-sqids
包简介
Laravel wrapper for Sqids to obscure database IDs with unique, URL-safe identifiers.
关键字:
README 文档
README
Laravel Sqids is a lightweight wrapper around Sqids (pronounced "squids"), an open-source library for generating short, URL-safe, non-sequential, and unique identifiers from numbers.
This package simplifies the integration of Sqids into your Laravel application, providing a clean and efficient way to obscure raw database IDs in URLs, forms, or other scenarios where unique, aesthetically pleasing identifiers are needed without storing them in the database.
✨ Features:
- Unique IDs: Generates short, collision-free IDs that are unique to your application.
- Model-Specific IDs: Produces distinct IDs for each model to ensure uniqueness across models.
- Route Model Binding: Automatically resolves route model bindings with the generated IDs.
- Convenient Helpers: Includes query scopes for easy filtering and an option to configure numeric-only IDs.
Installation
Install the package via Composer:
composer require istiak-tridip/laravel-sqids
Important
This package requires PHP 8.3 or higher and is compatible with Laravel 11.x, 12.x and 13.x.
If you need to customize the default configuration, publish the config file:
php artisan vendor:publish --tag=sqids-config
🚀 Getting Started
To use Sqids with a model, simply add the HasSqids trait to your model:
use Istiak\Sqids\Concerns\HasSqids; class User extends Model { use HasSqids; }
👩💻 Usage
🛠️ Accessing Sqids
You can access the Sqid directly from the model:
$user = User::query()->first(); echo $user->refid; // Outputs the Sqid for the model's ID
If needed, you can decode the Sqid back to the original ID:
// Throws an exception on failure $id = $user->sqids()->decode($user->refid); // Returns null on failure $id = $user->sqids()->decodeOrNull($user->refid);
🔍 Query Helpers
When querying models using Sqids, you can use the provided query scopes instead of manually decoding the Sqids:
// Find a single model by Sqid $user = User::query()->whereSqid($sqid)->first(); // Find multiple models by Sqids $users = User::query()->whereSqidIn([$sqid1, $sqid2])->get();
🔗 Route Model Binding
The package automatically resolves route model bindings using Sqids. You can define your routes as usual:
// GET /invoices/86Rf07xd4z Route::get('/invoices/{record}', function (Invoice $record) { return $record->number; });
In this example, the Invoice model will be resolved using the Sqid provided in the route.
🔢 Generating Numeric IDs
If you need numeric-only Sqids (e.g., 4622014635), configure the package to use a numeric alphabet in the AppServiceProvider's boot method:
use Istiak\Sqids\Support\Config; class AppServiceProvider extends ServiceProvider { public function boot() { Config::generateNumericIds(); } }
This ensures all generated Sqids consist only of numbers.
🔄 Alternatives
If this package doesn’t meet your needs, here are some alternative packages you can explore:
-
red-explosion/laravel-sqids Another awesome Laravel package for integrating Sqids, offering a different implementation approach and features like prefixed Sqids.
-
sqids/sqids-php The official PHP implementation of Sqids. Ideal for those who don’t require Laravel-specific integrations.
📜 License
Laravel Sqids was created by Istiak Tridip and is open-sourced under the MIT License.
istiak-tridip/laravel-sqids 适用场景与选型建议
istiak-tridip/laravel-sqids 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9.67k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2025 年 04 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「laravel」 「sqids」 「laravel-sqids」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 istiak-tridip/laravel-sqids 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 istiak-tridip/laravel-sqids 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 istiak-tridip/laravel-sqids 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Obfuscate incrementing IDs with Sqids for Laravel and Eloquent.
Sqids for Laravel.
A Laravel Filament package that automatically encodes and decodes model IDs using sqids/sqids for improved security and aesthetics.
Alfabank REST API integration
Backport of sqids/sqids for PHP 7.4 and PHP 8.0
A comprehensive unique identifier generation library supporting UUID, ULID, Snowflake, NanoID, Sqids, KSUID, CUID2, TypeID, XID, ObjectID, PushID, and Timeflake
统计信息
- 总下载量: 9.67k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-04-14