rorecek/laravel-autoincrement
Composer 安装命令:
composer require rorecek/laravel-autoincrement
包简介
Set table AUTO_INCREMENT values in your Laravel application
README 文档
README
This package will help you set table AUTO_INCREMENT values in your Laravel application.
Why do you need to change auto-increment values?
If you do not change auto-increment values of your tables you are exposing
how many records you have in there. Imagine some user makes an order and
is redirected to this order detail URL https://example.com/orders/10.
User will know then that there are only 10 orders in the whole system which
is probably information you do not want to share. If you will change your
table to start with a higher auto-increment number, for example, 512322,
it will not be that obvious (https://example.com/orders/512332).
But what if he makes another order month from now? There will be only small difference between these numbers and anyone can easily calculate your monthly activity. There is an easy solution for this. Add auto-increment update toLaravel's command scheduler and add some random number to your next auto-increment id automatically on hourly, daily, monthly, etc., basis.
What are the benefits?
-
Integer values are way more efficient than UUIDs or other types of string ids
-
Sorting by id still leads to chronological order
-
You do not expose the total number of resources in your database, for example
https://example.com/users/10
Installation
You can install this package via composer using this command:
composer require rorecek/laravel-autoincrement
Usage
You can use this package to set the next auto-increment id to a specific value, or to add a number to current value or to reset auto-increment to the lowest possible value.
AutoIncrement::table('items')->set(100); // Set table auto-increment value to 100 AutoIncrement::table('items')->add(500); // Increase current auto-increment value by 500 AutoIncrement::table('items')->addRandomBetween(10, 100); // Increase current auto-increment value by random number between 10 and 100 AutoIncrement::table('items')->reset(); // Reset auto-increment to the lowest value possible taking existing records in consideration.
Migrations
Set auto-increments directly in your migrations.
Schema::create('items', function (Blueprint $table) { $table->bigIncrements('id'); $table->timestamps(); }); AutoIncrement::table('items')->set(101); // Next auto-increment id will be 101
Scheduler
Setup automatic auto-increment increases using task scheduler.
// App\Console\Kernel.php protected function schedule(Schedule $schedule) { $schedule->call(function () { AutoIncrement::table('messages')->add(35); })->hourly(); $schedule->call(function () { AutoIncrement::table('registrations')->addRandomBetween(10, 100); AutoIncrement::table('bookings')->addRandomBetween(100, 500); })->daily(); }
Advanced usage
You can optionally use different connections and/or closures if needed.
AutoIncrement::connection('foo')->table('items')->... // Using different connection AutoIncrement::table('items')->set(function () { return (int) date('ymd') . 1001; }); // Using closure
Support
If you believe you have found an issue, please report it using the GitHub issue tracker, or better yet, fork the repository and submit a pull request.
If you're using this package, I'd love to hear your thoughts. Thanks!
License
The MIT License (MIT). Pavel Rorecek
rorecek/laravel-autoincrement 适用场景与选型建议
rorecek/laravel-autoincrement 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 225 次下载、GitHub Stars 达 12, 最近一次更新时间为 2019 年 08 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「mysql」 「id」 「laravel」 「autoincrement」 「auto-increment」 「auto_increment」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rorecek/laravel-autoincrement 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rorecek/laravel-autoincrement 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rorecek/laravel-autoincrement 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Automates the process of updating assets version in Symfony2 & Symfony3 projects
Facilitates the generation and autocompletion of a sequential value in the database
Facilitates the generation and autocompletion of a sequential value in the database
Dibi is Database Abstraction Library for PHP
Automates the process of updating assets version in Symfony 5 projects still using parameters.yml
Facilitates the generation and autocompletion of a sequential value in the database
统计信息
- 总下载量: 225
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-29