PHP code example of highsolutions / eloquent-sequence

1. Go to this page and download the library: Download highsolutions/eloquent-sequence library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

highsolutions / eloquent-sequence example snippets


use HighSolutions\EloquentSequence\Sequence;

class Section extends Model {

    use Sequence;

    public function sequence()
    {
        return [
            'group' => 'article_id',
            'fieldName' => 'seq',
        ];
    }
}

$section = Section::create([
    'article_id' => 1,
    'title' => 'Lorem ipsum',
]);

{
    'id' => 1,
    'article_id' => 1,
    'title' => 'Lorem ipsum',
    'seq' => 1
}

Section::create([
    'article_id' => 1,
    'title' => 'Lorem ipsum Second',
]);
Section::create([
    'article_id' => 2,
    'title' => 'Lorem ipsum Third but new',
]);

[{
    '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
}]

Section::find(1)->delete();

[{
    'id' => 2,
    'article_id' => 1,
    'title' => 'Lorem ipsum Second',
    'seq' => 1
}, {
    'id' => 3,
    'article_id' => 2,
    'title' => 'Lorem ipsum Third but new',
    'seq' => 1
}]

Section::where('article_id', 1)->orderBy('seq', 'asc')->get();

Section::where('article_id', 1)->sequenced()->get();
Section::where('article_id', 1)->sequenced('desc')->get();

Section::find(2)->up();

Section::find(2)->down();

Section::find(2)->moveToFirst();

Section::find(2)->moveToLast();

Section::find(2)->move(5);

Section::refreshSequence();

Section::find(2)->isFirst();

Section::find(2)->isNotFirst();

Section::find(2)->isLast();

Section::find(2)->isNotLast();