News

Create Model Sequences With the Eloquent Sequencer Package

Published
Create Model Sequences With the Eloquent Sequencer Package image

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:

use Gurgentil\LaravelEloquentSequencer\Traits\Sequenceable;
use Illuminate\Database\Eloquent\Model;
 
class Task extends Model
{
use Sequenceable;
 
protected static $fillable = [
'position',
];
 
protected static $sequenceableKeys = [
'task_list_id',
];
 
public function taskList()
{
return $this->belongsTo(TaskList::class);
}
}

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:

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

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

$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.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

tinkerwell logo
Tinkerwell

Enjoy coding and debugging in an editor designed for fast feedback and quick iterations. It's like a shell for your application – but with multi-line editing, code completion, and more.

Visit Tinkerwell

The latest

View all →
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article
Laravel AI SDK Adds Human-in-the-Loop Tool Approval image

Laravel AI SDK Adds Human-in-the-Loop Tool Approval

Read article
Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals image

Pest 5 Released With Test Impact Analysis, Agent Verification, and Evals

Read article
Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs image

Queue-SQL: Run Mass Deletes and Updates Across Parallel Queue Jobs

Read article
Blade Formatting in Laravel Pint image

Blade Formatting in Laravel Pint

Read article
Inertia DevTools Is Now on the Chrome Web Store image

Inertia DevTools Is Now on the Chrome Web Store

Read article