testmonitor/eloquent-incrementable
Composer 安装命令:
composer require testmonitor/eloquent-incrementable
包简介
Define a custom auto-increment field in your Eloquent model, that is determined through PHP rather than your database engine.
README 文档
README
Define a custom auto-increment field in your Eloquent model, that is determined through PHP rather than your database engine.
Furthermore, by making use of increment groups, you can restart counting in-table based on other fields. Consider this example:
| id | code | project_id |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 1 |
| 3 | 3 | 1 |
| 4 | 1 | 2 |
| 5 | 2 | 2 |
Imagine a bug tracking application that stores each bug in a single table, but is represented
on a per-project basis. You'll want start each project with a fresh bug count, while maintaining
a unique database id. Incrementable will enable you to automatically reset the code counter
once a new project_id is defined.
Table of Contents
Installation
This package can be installed through Composer:
$ composer require testmonitor/eloquent-incrementable
Usage
In order to add Incrementable to your Eloquent model, you'll need to:
- Use the trait
TestMonitor\Incrementable\Traits\Incrementableon your model(s). - Configure the incrementable field (note: make sure its an integer column).
- Optionally, add one or more increment groups.
Add the Incrementable trait on the models you want to track:
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; use TestMonitor\Incrementable\Traits\Incrementable; class Bug extends Model { use Incrementable, SoftDeletes; protected $table = 'bugs'; protected $incrementable = 'code'; // This will cause the code to reset once // a new project_id is found. protected $incrementableGroup = ['project_id']; }
In order to avoid collisions, Incrementable will preserve the count for a soft-deleted model. Although this will cause a gap between this and the next model, it will ensure uniqueness when the model is restored.
Examples
In this example, we have set up the following:
- A table containing a
nameandcodefield. - An Eloquent model called
App\Bug, which uses the Incrementable trait - A property on the Bug model:
$incrementable = 'code'
We can now run this example:
$bug = new App\Bug(['name' => 'It doesn\'t work.']); $bug->save(); // Will show '1' echo $bug->code; $bug = new App\Bug(['name' => 'It really doesn\'t work.']); $bug->save(); // Will show '2' echo $bug->code;
Tests
The package contains integration tests. You can run them using PHPUnit.
$ vendor/bin/phpunit
Changelog
Refer to CHANGELOG for more information.
Contributing
Refer to CONTRIBUTING for contributing details.
Credits
License
The MIT License (MIT). Refer to the License for more information.
testmonitor/eloquent-incrementable 适用场景与选型建议
testmonitor/eloquent-incrementable 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 31.76k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2019 年 07 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「model」 「laravel」 「eloquent」 「count」 「increment」 「testmonitor」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 testmonitor/eloquent-incrementable 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 testmonitor/eloquent-incrementable 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 testmonitor/eloquent-incrementable 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
A Laravel Eloquent model trait for translatable resource
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Interfaces for web resource models and services to retrieve and create them
Help to manage meta data on Laravel Eloquent model
A package to cast json fields, each sub-keys is castable
Laravel 5 - Repositories to the database layer
统计信息
- 总下载量: 31.76k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 30
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-07-14