gurgentil/laravel-eloquent-sequencer
Installation:
composer require gurgentil/laravel-eloquent-sequencer
php artisan vendor:publish --provider="Gurgentil\LaravelEloquentSequencer\LaravelEloquentSequencerServiceProvider"
Publish the config file to customize defaults (column name, initial value, strategy).
Model Setup:
Add $sequenceable = true; to your Eloquent model class.
Example:
use Gurgentil\LaravelEloquentSequencer\Traits\Sequenceable;
class Post extends Model
{
use Sequenceable;
// ...
}
First Use Case:
Save a new model instance—it will automatically assign a sequence value (default: position column, starting at 1).
$post = new Post(['title' => 'First Post']);
$post->save(); // Auto-assigns `position = 1`
config/eloquentsequencer.php for global settings.Sequenceable trait in Traits/Sequenceable.php for customization hooks.position).Basic Sequencing:
$sequenceable = true; in the model.strategy: 'always' to sequence on every save()/update().strategy: 'on_create' to sequence only during initial creation.Custom Column/Initial Value: Override defaults in the model:
class Post extends Model
{
use Sequenceable;
protected $sequenceColumn = 'order';
protected $sequenceInitialValue = 100;
}
Manual Sequencing: Trigger sequencing manually:
$post->sequence(); // Assigns next value in sequence
Bulk Operations:
Use sequenceMany() for batch inserts:
Post::sequenceMany([['title' => 'Post 1'], ['title' => 'Post 2']]);
Integration with Events:
Listen to sequenced events for post-sequencing logic:
Post::created(function ($post) {
if ($post->wasRecentlyCreated) {
// Handle new sequence assignment
}
});
Dynamic Sequences:
Override getNextSequenceValue() to generate values dynamically:
protected function getNextSequenceValue()
{
return Carbon::now()->timestamp;
}
Conditional Sequencing:
Use shouldSequence() to control when sequencing occurs:
protected function shouldSequence()
{
return $this->isPublished();
}
Soft Deletes + Sequencing: Re-sequence after soft deletes to fill gaps:
Post::withTrashed()->orderBy('position')->get();
API Responses: Include sequence values in API responses:
return PostResource::collection(Post::latest()->get());
Column Mismatch:
position).$sequenceColumn in the model.Race Conditions:
save() calls may overwrite sequences if using strategy: 'always'.strategy: 'on_create' or implement optimistic locking.Initial Value Conflicts:
1 may cause issues if records exist with manual position values.sequenceInitialValue higher (e.g., 1000) or use DB::transaction() to reset sequences.Soft Deletes:
forceDelete().Caching:
php artisan cache:clear
Log Sequencing: Enable debug mode in config:
'debug' => true,
Check logs for sequencing events.
Check Next Value: Inspect the raw SQL for sequence queries:
\DB::enableQueryLog();
$post->sequence();
dd(\DB::getQueryLog());
Test Edge Cases:
Custom Strategies:
Extend the Strategy class to add new sequencing logic (e.g., "gap-fill" for soft deletes).
Hooks: Override these methods in your model:
getNextSequenceValue(): Custom value logic.shouldSequence(): Conditional sequencing.sequenced(): Post-sequencing actions.Database-Agnostic: The package uses raw queries for sequence management. For complex setups, override:
protected function getNextSequenceQuery()
{
return DB::table($this->getTable())
->where($this->sequenceColumn, '>', $this->{$this->sequenceColumn})
->min($this->sequenceColumn);
}
Batch Optimization: For large datasets, use chunking:
Post::chunk(100, function ($posts) {
foreach ($posts as $post) {
$post->sequence();
$post->save();
}
});
always > on_create > on_update > never.How can I help you explore Laravel packages today?