Package Data | |
---|---|
Maintainer Username: | HighSolutions |
Maintainer Contact: | adam@highsolutions.pl (HighSolutions) |
Package Create Date: | 2016-06-20 |
Package Last Update: | 2024-04-22 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-23 03:24:27 |
Package Statistics | |
---|---|
Total Downloads: | 101,301 |
Monthly Downloads: | 1,615 |
Daily Downloads: | 69 |
Total Stars: | 120 |
Total Watchers: | 5 |
Total Forks: | 12 |
Total Open Issues: | 0 |
Easy creation and management sequence support for Eloquent models with elastic configuration.
This package can be installed through Composer:
composer require highsolutions/eloquent-sequence
Or by adding the following line to the require
section of your Laravel webapp's composer.json
file:
"require": {
"HighSolutions/eloquent-sequence": "3.0.*"
}
Run composer update
to install the package.
Laravel | Eloquent-Sequence :---------|:---------- 5.1.x | 2.1.x 5.2.x | 2.2.x 5.3.x | 2.3.x 5.4.x | 2.4.x 5.5.x | 2.5.x 5.6.x | 2.6.x 5.x.x | 3.0.x
use HighSolutions\EloquentSequence\Sequence;
class Section extends Model {
use Sequence;
public function sequence()
{
return [
'group' => 'article_id',
'fieldName' => 'seq',
];
}
}
Note: as a field name do not use name of any exisiting method in that class, including sequence
, as this will not work.
You can specify four parameters:
group
- field name or field names (then input as an array) which narrows list of objects within sequence parameter will be calculated
fieldName
- field name in model to store sequence attribute
exceptions
- set this true if you want to catch exceptions during up/down methods
orderFrom1
- set this true if your list starts from 1 not 0 / used for move method
notUpdateOnDelete
- set this true if you don't want to update sequence attributes when delete an object
Sequence attribute will be set during model creation.
$section = Section::create([
'article_id' => 1,
'title' => 'Lorem ipsum',
]);
After this metod field values of $section
will be looking as:
{
'id' => 1,
'article_id' => 1,
'title' => 'Lorem ipsum',
'seq' => 1
}
When we create another Section objects:
Section::create([
'article_id' => 1,
'title' => 'Lorem ipsum Second',
]);
Section::create([
'article_id' => 2,
'title' => 'Lorem ipsum Third but new',
]);
We get list of objects with fields:
[{
'id' => 1,
'article_id' => 1,
'title' => 'Lorem ipsum',
'seq' => 1
}, {
'id' => 2,
'article_id' => 1,
'title' => 'Lorem ipsum Second',
'seq' => 2
}, {
'id' => 3,
'article_id' => 2,
'title' => 'Lorem ipsum Third but new',
'seq' => 1
}]
But when we delete object:
Section::find(1)->delete();
Sequence values will be updated accordingly:
[{
'id' => 2,
'article_id' => 1,
'title' => 'Lorem ipsum Second',
'seq' => 1
}, {
'id' => 3,
'article_id' => 2,
'title' => 'Lorem ipsum Third but new',
'seq' => 1
}]
To get object just add ->orderBy('seq', 'asc')
method:
Section::where('article_id', 1)->orderBy('seq', 'asc')->get();
or with local scope sequenced
:
Section::where('article_id', 1)->sequenced()->get();
Section::where('article_id', 1)->sequenced('desc')->get();
To move object one position higher (swap position with earlier object) you only need to:
Section::find(2)->up();
This will set sequence attribute to one position lower and object one position lower will have sequence attribute changed to one position further.
Narrowing groups from configuration will be of course used.
The same you can do it to make your object next in line:
Section::find(2)->down();
This will set Section ID=2 with sequence attribute like next Section object (based on sequence attribute) and swap their values accordingly.
To move object to the first position, you only need to:
Section::find(2)->moveToFirst();
This will set sequence attribute to the first position in the sequence and will reorder the objects between the original position and the first position accordingly.
Narrowing groups from configuration will be of course used.
To move object to the last position, you only need to:
Section::find(2)->moveToLast();
This will set sequence attribute to the last position in the sequence and will reorder the objects between the original position and the last position accordingly.
Narrowing groups from configuration will be of course used.
You are able to move object to another position also. This is very useful when you are implementing drag&drop functionality.
Section::find(2)->move(5);
This will set Section ID=2 with sequence attribute to 5th and rest objects' sequence attribute will be updated to match proper order.
Sometimes you may need to recalculate all position for given model (e.g. because of manually manipulating dataset). You can do it easily via:
Section::refreshSequence();
This static method will recalculate sequence attributes for every record for this model. Narrowing groups will be used as well as current sequence attribute of every record.
Run the tests with:
vendor/bin/phpunit
3.1.0
3.0.2
3.0.0
2.6.2
NotUpdateOnDelete
configuration parameter2.6.0
2.5.0
2.4.0
2.3.0
2.2.0
2.1.0
2.0.3
2.0.2
moveToFirst
and moveToLast
2.0.1
InvalidArgumentException
when position argument is lower than first possible sequence value for move
method2.0.0
InvalidArgumentException
when position argument is bigger than last possible sequecne value for move
methodmove
with invalid position argument (with exceptions parameter disabled) sets sequence attribute to first possible number (count + 1), instead of setting value from argument / BREAKING CHANGE1.3.1
down
method when used on object with seq > 2 (bug introduced in 1.3.0)1.3.0
up
method when used on object with seq > 21.2.0
1.1.1
1.1.0
1.0.0
0.9.0
This package is developed by HighSolutions, software house from Poland in love in Laravel.