|
| 1 | +# Incrementable Eloquent models |
| 2 | + |
| 3 | +[](https://packagist.org/packages/bytestgear/eloquent-incrementable) |
| 4 | +[](https://travis-ci.org/byTestGear/eloquent-incrementable) |
| 5 | +[](https://scrutinizer-ci.com/g/byTestGear/eloquent-incrementable/?branch=master) |
| 6 | +[](https://styleci.io/repos/89586066) |
| 7 | +[](https://packagist.org/packages/eloquent-incrementable) |
| 8 | + |
| 9 | +Define a custom auto-increment field in your Eloquent model, that is determined through PHP |
| 10 | +rather than your database engine. |
| 11 | + |
| 12 | +Furthermore, by making use of increment groups, you can restart counting in-table based on |
| 13 | +other fields. Consider this example: |
| 14 | + |
| 15 | +| id | **code** | project_id | |
| 16 | +|----|:--------:|:----------:| |
| 17 | +| 1 | **1** | 1 | |
| 18 | +| 2 | **2** | 1 | |
| 19 | +| 3 | **3** | 1 | |
| 20 | +| 4 | **1** | 2 | |
| 21 | +| 5 | **2** | 2 | |
| 22 | + |
| 23 | +Imagine a bug tracking application that stores each bug in a single table, but is represented |
| 24 | +on a per-project basis. You'll want start each project with a fresh bug count, while maintaining |
| 25 | +a unique database id. Incrementable will enable you to automatically reset the `code` counter |
| 26 | +once a new `project_id` is defined. |
| 27 | + |
| 28 | +## Table of Contents |
| 29 | + |
| 30 | +- [Installation](#installation) |
| 31 | +- [Usage](#usage) |
| 32 | +- [Examples](#examples) |
| 33 | +- [Tests](#tests) |
| 34 | +- [Changelog](#changelog) |
| 35 | +- [Contributing](#contributing) |
| 36 | +- [Credits](#credits) |
| 37 | +- [License](#license) |
| 38 | + |
| 39 | +## Installation |
| 40 | + |
| 41 | +This package can be installed through Composer: |
| 42 | + |
| 43 | +```sh |
| 44 | +$ composer require bytestgear/eloquent-incrementable |
| 45 | +``` |
| 46 | + |
| 47 | +## Usage |
| 48 | + |
| 49 | +In order to add Incrementable to your Eloquent model, you'll need to:<br /> |
| 50 | + |
| 51 | +1. Use the trait ```ByTestGear\Incrementable\Traits\Incrementable``` on your model(s). |
| 52 | +2. Configure the incrementable field *(note: make sure its an integer column)*. |
| 53 | +3. Optionally, add one or more increment groups. |
| 54 | + |
| 55 | +Add the Incrementable trait on the models you want to track: |
| 56 | + |
| 57 | +```php |
| 58 | +use Illuminate\Database\Eloquent\Model; |
| 59 | +use Illuminate\Database\Eloquent\SoftDeletes; |
| 60 | +use ByTestGear\Incrementable\Traits\Incrementable; |
| 61 | + |
| 62 | +class Bug extends Model |
| 63 | +{ |
| 64 | + use Incrementable, SoftDeletes; |
| 65 | + |
| 66 | + protected $table = 'bugs'; |
| 67 | + |
| 68 | + protected $incrementable = 'code'; |
| 69 | + |
| 70 | + // This will cause the code to reset once |
| 71 | + // a new project_id is found. |
| 72 | + protected $incrementableGroups = ['project_id']; |
| 73 | +} |
| 74 | +``` |
| 75 | + |
| 76 | +In order to avoid collisions, Incrementable will preserve the count for a |
| 77 | +soft-deleted model. Although this will cause a gap between this and the |
| 78 | +next model, it will ensure uniqueness when the model is restored. |
| 79 | + |
| 80 | +## Examples |
| 81 | + |
| 82 | +In this example, we have set up the following: |
| 83 | + |
| 84 | +- A table containing a `name` and `code` field. |
| 85 | +- An Eloquent model called `App\Bug`, which uses the Incrementable trait |
| 86 | +- A property on the Bug model: `$incrementable = 'code'` |
| 87 | + |
| 88 | +We can now run this example: |
| 89 | + |
| 90 | +```php |
| 91 | +$bug = new App\Bug(['name' => 'It doesn\'t work.']); |
| 92 | +$bug->save(); |
| 93 | + |
| 94 | +// Will show '1' |
| 95 | +echo $bug->code; |
| 96 | + |
| 97 | +$bug = new App\Bug(['name' => 'It really doesn\'t work.']); |
| 98 | +$bug->save(); |
| 99 | + |
| 100 | +// Will show '2' |
| 101 | +echo $bug->code; |
| 102 | +``` |
| 103 | + |
| 104 | +## Tests |
| 105 | + |
| 106 | +The package contains integration tests. You can run them using PHPUnit. |
| 107 | + |
| 108 | +``` |
| 109 | +$ vendor/bin/phpunit |
| 110 | +``` |
| 111 | + |
| 112 | +## Changelog |
| 113 | + |
| 114 | +Refer to [CHANGELOG](CHANGELOG.md) for more information. |
| 115 | + |
| 116 | +## Contributing |
| 117 | + |
| 118 | +Refer to [CONTRIBUTING](CONTRIBUTING.md) for contributing details. |
| 119 | + |
| 120 | +## Credits |
| 121 | + |
| 122 | +- [Thijs Kok](https://www.testmonitor.com/) |
| 123 | +- [Stephan Grootveld](https://www.testmonitor.com/) |
| 124 | +- [Frank Keulen](https://www.testmonitor.com/) |
| 125 | +- [All Contributors](../../contributors) |
| 126 | + |
| 127 | +## License |
| 128 | + |
| 129 | +The MIT License (MIT). Refer to the [License](LICENSE.md) for more information. |
0 commit comments