Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Column Sortable

Column Sortable Laravel Package

kyslik/column-sortable

Add sortable table columns to Laravel Eloquent listings. Define sortable fields on models, generate Blade sort links with direction icons, and sort by related hasOne/belongsTo columns or withCount aliases. Works with Laravel 5.5–8 (via package versions).

View on GitHub
Deep Wiki
Context7

Package for handling column sorting in Laravel 6.x

Frequently asked questions about Column Sortable
Does this package work with Laravel 11, 12, or 13?
The package officially supports Laravel 5.5–10, but community contributions have extended compatibility to Laravel 11/12/13. Test thoroughly, especially if using newer Eloquent features. Check the GitHub issues for version-specific patches.
How do I define sortable columns for a model?
Use the `$sortable` property in your model to define columns. For example, `$sortable = ['name', 'created_at']` enables sorting on those fields. For relations, use dot notation like `'user.name'` or `'posts.count'`.
Can I customize the sort direction icons (e.g., replace Font Awesome with Bootstrap icons)?
Yes, the package relies on Font Awesome 4/5 by default, but you can override the icon classes in the config file. Replace `fa-sort`, `fa-sort-up`, and `fa-sort-down` with your preferred icon classes.
Will this package slow down my application if I have millions of rows?
Sorting large datasets can impact performance. Pre-defining `$sortable` avoids runtime schema checks, and adding database indexes on sorted columns is highly recommended. For extreme cases, consider custom queries via the `overrideSortable` method.
How do I sort by a `withCount` relationship (e.g., `posts` count)?
Use the `withCount` alias in your `$sortable` array. For example, `$sortable = ['posts']` will sort by the count of related `posts`. The package automatically handles the `withCount` query transformation.
Is there a way to sort by a `hasOne` or `belongsTo` relation field (e.g., `user.name`)?
Yes, define the relation in your model (e.g., `public function user() { return $this->belongsTo(User::class); }`) and use dot notation in `$sortable`, like `'user.name'`. The package generates the proper join and sort query.
What happens if a user tries to sort by a non-existent column?
The package throws a `ColumnSortableException`. Catch this exception in your controller or middleware to handle invalid sort requests gracefully, such as redirecting to a default sort or logging the error.
Can I override the default sorting behavior for specific queries?
Yes, use the `overrideSortable` method in your query builder. This is useful for complex scenarios like custom joins or subqueries. Example: `$query->overrideSortable(fn($query) => $query->join(...))`.
Does this package support Laravel’s pagination and sorting simultaneously?
Absolutely. The `@sortablelink` Blade directive integrates seamlessly with Laravel’s pagination. Just ensure your controller returns a paginated query with sorting applied, and the directive will handle the rest.
Are there any alternatives to this package for Laravel column sorting?
Alternatives include `spatie/laravel-query-builder` (for advanced query filtering/sorting) or `darkaonline/l5-swagger` (if API sorting is needed). However, `kyslik/column-sortable` is uniquely optimized for Eloquent models with Blade UI integration and relation support.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
nexmo/api-specification
capell-app/block-library
axium/identity
cetria/laravel-dummy-models
cetria/reflection-helper
agropredict/sso-auth-bundle
evolvestudio/spam-protection
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
ecotone/kafka
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata