kyslik/column-sortable
Add sortable table columns to Laravel 5.5–8. Generate clickable links in Blade, configure sortable fields and icons, and sort by related hasOne/belongsTo attributes or withCount(). Works seamlessly with pagination and query building.
Sortable trait extends Eloquent queries with sorting capabilities, aligning with Laravel’s query builder patterns.@sortablelink directive integrates seamlessly with Blade templates, reducing boilerplate for UI-level sorting controls. This is particularly useful for admin panels or data tables.^6.0 version constraint simplifies dependency management.hasOne, belongsTo) via method chaining, reducing the need for custom query logic in most cases.hasMany) may lead to runtime errors. Requires careful testing of edge cases (e.g., self-referencing models).Schema::hasColumn) add minor DB queries per request. Mitigated by defining $sortable arrays explicitly.sort parameters in URLs could be exploited (e.g., SQL injection via malformed input). The package’s ColumnSortableException (code 0) helps mitigate this, but application-level validation is recommended.$sortable arrays or Schema::hasColumn calls materially affect query performance? Benchmark with production-like data.ColumnSortableException be surfaced to users (e.g., 400 errors for invalid sorts)?$sortable, malformed URLs).User) to validate integration and performance.@sortablelink directive in a non-critical view.php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider" --tag="config") and customize:
asc/desc).Sortable trait and define $sortable arrays for target models.hasOne/belongsTo methods are defined.orderBy calls with $model->sortable()->paginate().// Before
$users = User::orderBy(request('sort_column', 'name'))->paginate(10);
// After
$users = User::sortable()->paginate(10);
@sortablelink directives.@sortablelink('name', 'Username')
@sortablelink('created_at', 'Joined')
ColumnSortableException gracefully.try {
$data = Model::sortable()->paginate(10);
} catch (ColumnSortableException $e) {
return back()->withError('Invalid sort parameter');
}
^6.6.0; for 12+, use ^7.0.0; for 13+, use ^8.0.0.composer require kyslik/column-sortable:^6.0
config/columnsortable.php.Sortable trait and $sortable arrays to pilot models.@sortablelink directives.name).user.name).$sortable, invalid URLs).columnsortable.php) reduces maintenance overhead compared to scattered logic.ColumnSortableException provides actionable error codes (0: URL parsing, 1: invalid relation, 2: unsupported relation type).users.name or posts.title; avoid unsupported relations."$sortable arrays eliminate Schema::hasColumn calls, reducing DB hits.ORDER BY clauses, which may impact performance on large datasets. Indexes on sortable columns are critical.created_at is indexed if frequently sorted.| Failure Scenario | Impact | Mitigation |
|---|---|---|
Malformed sort URL parameter |
ColumnSortableException (code 0) |
Validate input; return 400 errors for invalid sorts. |
| Invalid relation in sort parameter | ColumnSortableException (code 1) |
Log and ignore; default to a safe sort column (e.g., id). |
| Unsupported relation type | ColumnSortableException (code 2) |
Document supported relations; override for custom logic. |
| Missing indexes on sortable columns | Slow queries | Add indexes; monitor query performance with Laravel Debugbar or Query Logger. |
| Package version incompatibility | Broken functionality | Pin to a stable version |
How can I help you explore Laravel packages today?