shoboske / laravel-data-table-query-builder
A package for managing the backend of a datatable request, sorting, order, serverside pagination
Package info
github.com/shoboske/laravel-datatable-query-builder
pkg:composer/shoboske/laravel-data-table-query-builder
Fund package maintenance!
Requires
- php: ^8.4
- illuminate/contracts: ^11.0||^12.0||^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.8
- orchestra/testbench: ^11.0.0||^10.0.0||^9.0.0
- pestphp/pest: ^4.0
- pestphp/pest-plugin-arch: ^4.0
- pestphp/pest-plugin-laravel: ^4.0
- phpstan/extension-installer: ^1.4
- phpstan/phpstan-deprecation-rules: ^2.0
- phpstan/phpstan-phpunit: ^2.0
This package is auto-updated.
Last update: 2026-07-03 21:14:00 UTC
README
This package adds a reusable Eloquent scope for building datatable queries. It supports selecting searchable columns, applying relationship-aware filtering, ordering by a requested column, and eager-loading relationships used by your datatable.
Installation
You can install the package via composer:
composer require shoboske/laravel-data-table-query-builder
You can publish the config file with:
php artisan vendor:publish --tag="data-table-query-builder-config"
This is the contents of the published config file:
return [ // SQL operator used when filtering searchable columns. 'like_term' => 'like', // Default sort direction when no explicit direction is provided. 'default_sort_direction' => 'asc', 'models' => [ // Attribute name used to mark columns as searchable in a model. 'search_term' => 'searchable', // Attribute name used when a column should be selected under an alias. 'alias' => 'alias', // Default column used when no sort column is supplied. 'default_sort_column_name' => 'id', ], 'query_params' => [ // Query parameter that controls how many records are returned. 'take' => 'take', // Default number of records to return when the take parameter is missing. 'default_take' => 10, // Query parameter that controls how many records are skipped. 'skip' => 'skip', // Query parameter that contains the search term. 'search' => 'search', // Query parameter that contains the column to sort by. 'sort' => 'sort', // Query parameter that contains the sort direction. 'direction' => 'direction', ], 'response_keys' => [ // Response key that contains the paginated data. 'data' => 'data', // Response key that contains the total result count. 'count' => 'count', ], ];
Usage
Add the DataTableQueryBuilderTrait to your model and define the two required methods.
use Illuminate\Database\Eloquent\Model; use Shoboske\DataTableQueryBuilder\Traits\DataTableQueryBuilderTrait; class User extends Model { use DataTableQueryBuilderTrait; protected function getDataTableColumns(): array { return [ 'name' => [ 'searchable' => true, ], 'email' => [ 'searchable' => true, ], ]; } protected function getDataTableRelationships(): array { return [ "belongsTo" => [ "role" => [ "model" => 'role_id' 'columns' => [ 'role' => [ 'searchable' => true, 'orderable' => true ] ] ] ] ]; } }
You can generate those methods automatically with the included command:
php artisan data-table:add-trait App\\Models\\User
Then use the scope in your controller or query layer:
$users = User::query() ->eloquentQuery('name', 'asc', request('search')) ->get();
If you need to add relationships for sorting or filtering, return them from getDataTableRelationships() and pass the relationship names to the scope as the last argument.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Inspiration
This package was inspired by James Dordoy's Laravel Vue Datatable package.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.