shoboske/laravel-data-table-query-builder

A package for managing the backend of a datatable request, sorting, order, serverside pagination

Maintainers

Package info

github.com/shoboske/laravel-datatable-query-builder

Homepage

pkg:composer/shoboske/laravel-data-table-query-builder

Transparency log

Fund package maintenance!

shoboske

Statistics

Installs: 8

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

dev-main 2026-07-03 21:13 UTC

This package is auto-updated.

Last update: 2026-07-03 21:14:00 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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.