Create Model Sequences With the Eloquent Sequencer Package


June 25th, 2020


Eloquent Sequencer is a package by Gustavo R. Gentil for automatically sequencing your models when adding new records. This package makes it trivial to rearrange things like todos, cards, or lists of models.

Here’s an example from the readme demonstrating a task list that has many Task models:

1use Gurgentil\LaravelEloquentSequencer\Traits\Sequenceable;
2use Illuminate\Database\Eloquent\Model;
4class Task extends Model
6 use Sequenceable;
8 protected static $fillable = [
9 'position',
10 ];
12 protected static $sequenceableKeys = [
13 'task_list_id',
14 ];
16 public function taskList()
17 {
18 return $this->belongsTo(TaskList::class);
19 }

Let’s say that you have a drag and drop interface for reordering tasks. You could call the following methods, and this package will automatically rearrange the sequence of tasks:

1// Other items in the sequence will be rearranged
2$task->update(['position' => 4]);
4// Remaining tasks will be rearranged after removing a task

Finally, you’d use the following method to get the sequenced list of tasks:

1$tasks = Task::sequenced()->get();

Learn More

You can learn more about this package, get full installation instructions, and view the source code on GitHub at gurgentil/laravel-eloquent-sequencer.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.