- 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.