kyslik/column-sortable
Add sortable table columns to Laravel (5.5–8) Eloquent listings. Provides Blade helpers to generate sort links with direction icons, supports configurable sortable fields, pagination-friendly URLs, and sorting on hasOne/belongsTo relations with optional aliases.
Sortable trait extends Eloquent’s query builder with sorting capabilities, aligning with Laravel’s conventions.@sortablelink directive integrates seamlessly with Laravel’s Blade templating engine, reducing boilerplate for UI-level sorting controls.paginate()), and eager loading (with()), ensuring compatibility with existing data-fetching logic.hasOne/belongsTo relations, enabling sorting across related tables. Custom join logic can be overridden for complex scenarios (e.g., polymorphic relations).6.x for Laravel 8.x) and monitor deprecation notices.ColumnSortableException) and sanitize user input (e.g., sort query parameters).Laravel Version Alignment:
6.x for Laravel 8.x, 8.x for Laravel 10/11).kyslik/column-sortable:^8.0.Relation Sorting Scope:
user.detail.phone_number)? If so, validate performance impact with large datasets.with('detail') and nested relations to measure query plan complexity.UI/UX Consistency:
columnsortable.php to use Tailwind CSS classes instead.Security:
sort query parameters exposed to user input (e.g., via URLs)? Implement validation or middleware to prevent malformed input (e.g., sort=detail..phone_number).if (!in_array(request('sort'), ['id', 'name', 'detail.phone_number'])) {
abort(403);
}
Testing:
orderBy clauses).@sortablelink HTML attributes).name, email, created_at).price, name, rating).title, publish_date, author).orderBy calls in controllers).$sortable arrays or relation definitions.User) and replace manual sorting with the trait:
// Before
$users = User::orderBy(request('sort_column', 'name'))->paginate(10);
// After
$users = User::sortable()->paginate(10);
@sortablelink.6.x for Laravel 8.x or 8.x for Laravel 10/11.5.x–6.x branches.6.x branch.composer require kyslik/column-sortable:^6.0.php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider".use Sortable; and define $sortable arrays in target models.class User extends Model {
use Sortable;
public $sortable = ['id', 'name', 'email', 'created_at'];
}
orderBy with sortable():
public function index() {
return User::sortable()->paginate(10);
}
@sortablelink:
@sortablelink('name', 'Username')
@sortablelink('created_at', 'Joined')
?sort=name:desc).?sort=user.name).$sortable).orderBy logic in controllers.config/columnsortable.php.addressSortable()) may diverge from package updates.$sortable array or invalid relation names. Fix by defining $sortable or checking relation methods.addressSortable() overrides for custom logic.User::sortable()->toSql().try/catch (\Kyslik\ColumnSortable\Exceptions\ColumnSortableException).ALTER TABLE users ADD INDEX idx_name (name)).with() sparingly for relation sorting to avoid N+1 queries.Cache::remember()).How can I help you explore Laravel packages today?