承接 rorecek/laravel-autoincrement 相关项目开发

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

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

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

统计信息

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

GitHub 信息

  • Stars: 12
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-08-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固