Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Eloquent Incrementable Laravel Package

testmonitor/eloquent-incrementable

View on GitHub
Deep Wiki
Context7

Incrementable Eloquent models

Latest Stable Version StyleCI codecov License

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:

  1. Use the trait TestMonitor\Incrementable\Traits\Incrementable on your model(s).
  2. Configure the incrementable field (note: make sure its an integer column).
  3. 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 name and code field.
  • 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.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui
babelqueue/php-sdk
facebook/capi-param-builder-php
babelqueue/symfony
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours