czim/laravel-listify
Laravel package to manage ordered lists for Eloquent models. Provides helpers for positioning records, moving items up/down, reordering, and maintaining consistent sort indexes within groups/scopes. Useful for sortable menus, playlists, and drag-and-drop UIs.
Installation
composer require czim/laravel-listify
Publish the config file (if needed):
php artisan vendor:publish --provider="Czim\Listify\ListifyServiceProvider"
Basic Usage
Register a listable model (e.g., Post):
use Czim\Listify\Traits\Listable;
class Post extends Model
{
use Listable;
}
First Use Case Fetch a list of posts with default sorting:
$posts = Post::listify()->get();
Or with custom sorting:
$posts = Post::listify(['sort' => 'created_at:desc'])->get();
Configuration
Check config/listify.php for default settings (e.g., default_sort, allowed_sort_fields).
Dynamic Sorting
Use listify() with a sort parameter to override defaults:
// In a controller
$sort = request('sort', 'title:asc');
$items = Model::listify(['sort' => $sort])->get();
Filtering
Chain where clauses before listify():
$activePosts = Post::where('published', true)
->listify(['sort' => 'views:desc'])
->get();
Pagination Combine with Laravel’s pagination:
$posts = Post::listify()->paginate(10);
API Integration Return sorted/filtered lists in API responses:
return response()->json(Post::listify()->get());
Middleware for Sorting
Create middleware to handle sort query params globally:
public function handle($request, Closure $next)
{
if ($request->has('sort')) {
$model = app($request->route('model'));
$model::listify(['sort' => $request->sort]);
}
return $next($request);
}
Custom Sort Fields
Override allowed fields in config/listify.php:
'allowed_sort_fields' => [
'posts' => ['title', 'created_at', 'views'],
],
Eager Loading
Use with() before listify() to optimize queries:
$posts = Post::with('author')->listify()->get();
Testing
Mock listify() in unit tests:
$model = new Post();
$this->assertEquals(
['title:asc'],
$model->listify(['sort' => 'title:asc'])->getQuery()->orders
);
Case Sensitivity
Sort fields in config/listify.php must match the model’s fillable/attributes exactly (e.g., created_at vs createdAt).
Reserved Keywords
Avoid using sort as a custom column name—it conflicts with the package’s parameter.
Mass Assignment
Ensure sort fields are fillable or use $fillable in the model:
protected $fillable = ['title', 'views'];
Query Overrides
Chaining listify() after orderBy() will reset the sort order. Use listify() first:
// Correct
Post::listify(['sort' => 'title'])->get();
// Incorrect (overrides)
Post::orderBy('id')->listify()->get();
Inspect Queries Use Laravel’s query logging:
\DB::enableQueryLog();
$posts = Post::listify()->get();
dd(\DB::getQueryLog());
Validate Config
Check config/listify.php for typos in allowed_sort_fields or default_sort.
Sort Direction
Ensure asc/desc is lowercase and separated by a colon (field:asc).
Custom Sort Logic
Override the sortableFields() method in your model:
public function sortableFields()
{
return ['custom_field', 'parent->name'];
}
Dynamic Config
Use listify() with a closure for runtime config:
$posts = Post::listify(function ($query) {
$query->where('status', 'published');
return ['sort' => 'updated_at:desc'];
})->get();
Event Hooks
Listen for listify.before or listify.after events (if the package supports them):
\Event::listen('listify.before', function ($query, $options) {
// Modify query or options
});
Localization
Translate sort labels (e.g., "Newest" for created_at:desc) in your language files.
How can I help you explore Laravel packages today?