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 Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Eloquent Integration: The package leverages Laravel’s Eloquent ORM, making it a natural fit for applications using Eloquent models for data access. The Sortable trait extends Eloquent’s query builder with sorting capabilities, aligning with Laravel’s conventions.
  • Blade Integration: The @sortablelink directive integrates seamlessly with Laravel’s Blade templating engine, reducing boilerplate for UI-level sorting controls.
  • Configurability: The package’s modular design (e.g., column type classification, relation handling, and aliasing) allows customization to match application-specific requirements without forcing rigid patterns.

Integration Feasibility

  • Low Friction: Supports Laravel’s auto-discovery (v5.5+) and manual registration, with minimal setup (composer install + config publish). No database migrations or schema changes are required.
  • Query Builder Compatibility: Works with Laravel’s query builder, pagination (paginate()), and eager loading (with()), ensuring compatibility with existing data-fetching logic.
  • Relation Support: Handles hasOne/belongsTo relations, enabling sorting across related tables. Custom join logic can be overridden for complex scenarios (e.g., polymorphic relations).

Technical Risk

  • Deprecation Risk: The package supports Laravel 5.5–10 but has dropped support for Laravel <9 and PHP <8.0 in v7.0+. Risk: Applications using older Laravel versions (e.g., 5.x–8.x) may face compatibility issues with future updates. Mitigation: Pin to a stable minor version (e.g., 6.x for Laravel 8.x) and monitor deprecation notices.
  • Query Complexity: Advanced features (e.g., relation sorting, custom joins) may introduce performance overhead or SQL injection risks if not validated. Mitigation: Use the package’s built-in exception handling (ColumnSortableException) and sanitize user input (e.g., sort query parameters).
  • UI Dependencies: Relies on Font Awesome for icons. Risk: Breaking changes if the project migrates to a different icon library. Mitigation: Customize the config to use alternative classes or CSS.

Key Questions

  1. Laravel Version Alignment:

    • What is the target Laravel version for the application? Ensure the package version aligns (e.g., 6.x for Laravel 8.x, 8.x for Laravel 10/11).
    • Example: For Laravel 10, use kyslik/column-sortable:^8.0.
  2. Relation Sorting Scope:

    • Does the application frequently sort across relations (e.g., user.detail.phone_number)? If so, validate performance impact with large datasets.
    • Example: Test with with('detail') and nested relations to measure query plan complexity.
  3. UI/UX Consistency:

    • How should sorted columns be visually indicated? The package defaults to Font Awesome icons; ensure this aligns with the project’s design system.
    • Example: Customize columnsortable.php to use Tailwind CSS classes instead.
  4. Security:

    • Are 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).
    • Example: Add a middleware to whitelist allowed sort columns:
      if (!in_array(request('sort'), ['id', 'name', 'detail.phone_number'])) {
          abort(403);
      }
      
  5. Testing:

    • Are there existing tests for pagination/sorting logic? Add unit tests to verify:
      • Query builder output (e.g., orderBy clauses).
      • Blade directive rendering (e.g., @sortablelink HTML attributes).
      • Exception handling (e.g., invalid relations).

Integration Approach

Stack Fit

  • Primary Use Case: Ideal for admin panels, data tables, or search interfaces where column sorting is a core feature. Examples:
    • User management dashboards (sort by name, email, created_at).
    • E-commerce product grids (sort by price, name, rating).
    • CMS content listings (sort by title, publish_date, author).
  • Anti-Patterns: Avoid for:
    • High-frequency APIs where query complexity must be minimized.
    • Applications with dynamic schemas (e.g., noSQL or schema-less databases).

Migration Path

  1. Assessment Phase:
    • Audit existing sorting logic (e.g., manual orderBy calls in controllers).
    • Identify models requiring $sortable arrays or relation definitions.
  2. Pilot Integration:
    • Start with a single model (e.g., User) and replace manual sorting with the trait:
      // Before
      $users = User::orderBy(request('sort_column', 'name'))->paginate(10);
      
      // After
      $users = User::sortable()->paginate(10);
      
    • Update Blade templates to use @sortablelink.
  3. Gradual Rollout:
    • Prioritize high-impact tables (e.g., admin dashboards).
    • Replace custom query overrides with the package’s relation sorting where possible.
  4. Deprecation:
    • Phase out legacy sorting logic via feature flags or middleware.

Compatibility

  • Laravel Versions:
    • Recommended: Use 6.x for Laravel 8.x or 8.x for Laravel 10/11.
    • Legacy: For Laravel 5.5–7.x, pin to 5.x6.x branches.
  • Dependencies:
    • Requires PHP 8.0+ (v7.0+). For older PHP, use 6.x branch.
    • No conflicts with Laravel’s core or popular packages (e.g., Livewire, Filament).
  • Database:
    • No schema changes required. Works with any database supported by Laravel (MySQL, PostgreSQL, SQLite).

Sequencing

  1. Setup:
    • Install via Composer: composer require kyslik/column-sortable:^6.0.
    • Publish config: php artisan vendor:publish --provider="Kyslik\ColumnSortable\ColumnSortableServiceProvider".
  2. Model Integration:
    • Add use Sortable; and define $sortable arrays in target models.
    • Example:
      class User extends Model {
          use Sortable;
          public $sortable = ['id', 'name', 'email', 'created_at'];
      }
      
  3. Controller Updates:
    • Replace manual orderBy with sortable():
      public function index() {
          return User::sortable()->paginate(10);
      }
      
  4. Blade Updates:
    • Replace static links with @sortablelink:
      @sortablelink('name', 'Username')
      @sortablelink('created_at', 'Joined')
      
  5. Testing:
    • Validate:
      • Query parameters (e.g., ?sort=name:desc).
      • Relation sorting (e.g., ?sort=user.name).
      • Edge cases (e.g., invalid columns, empty $sortable).

Operational Impact

Maintenance

  • Pros:
    • Reduced Boilerplate: Eliminates repetitive orderBy logic in controllers.
    • Centralized Configuration: Sorting behavior (e.g., default direction, icons) is managed in config/columnsortable.php.
    • Community Support: 647 stars and active maintenance (last release: 2026-03-17).
  • Cons:
    • Configuration Drift: Custom overrides (e.g., addressSortable()) may diverge from package updates.
    • Dependency Risk: Future Laravel major versions may require package updates.

Support

  • Troubleshooting:
    • Common Issues:
      • 404 Errors: Missing $sortable array or invalid relation names. Fix by defining $sortable or checking relation methods.
      • SQL Errors: Complex joins may fail. Use addressSortable() overrides for custom logic.
      • UI Glitches: Font Awesome icons missing. Ensure the library is loaded or customize classes in config.
    • Debugging Tools:
      • Log queries: User::sortable()->toSql().
      • Catch exceptions: try/catch (\Kyslik\ColumnSortable\Exceptions\ColumnSortableException).
  • Documentation:
    • Strengths: Comprehensive README with examples for relations, aliasing, and overrides.
    • Gaps: Limited coverage of performance tuning (e.g., indexing strategies for sorted columns).

Scaling

  • Performance:
    • Best Practices:
      • Indexing: Ensure sorted columns are indexed in the database (e.g., ALTER TABLE users ADD INDEX idx_name (name)).
      • Eager Loading: Use with() sparingly for relation sorting to avoid N+1 queries.
      • Caching: Cache paginated results if sorting is infrequent (e.g., Cache::remember()).
    • Limitations:
      • Complex Relations: Sorting
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.
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle
codeflextech/permission-manager
karnoweb/livewire-datepicker
sayedenam/sayed-dashboard
milito/query-filter
apiboxsym/user-bundle
apiboxsym/health-check-bundle
jayeshmepani/jpl-moshier-ephemeris-php
elnasnato/laraliveui
labrodev/rest-sdk
sampaui/sampaui