yajra/laravel-datatables-fractal
Laravel DataTables plugin that transforms server-side JSON responses using League Fractal. Adds Fractal integration to yajra/laravel-datatables for cleaner, consistent API output. Supports PHP 8.2+ and Laravel 12.x.
yajra/laravel-datatables by integrating Fractal’s transformer pattern, enabling structured JSON responses without rewriting core query logic. Leverages DataTables’ server-side processing, pagination, and sorting while adding API-friendly serialization.include and fields query parameters (e.g., ?include=orders&fields[users]=id,name) for granular data exposure, critical for admin panels or APIs with role-based access.UserTransformer for both API and admin endpoints). Supports computed fields, access control, or multi-tenancy via transformer logic.yajra/laravel-datatables implementations. No forced architecture changes; works alongside Eloquent, API Resources, or custom query logic.ArraySerializer, ItemTransformer). Teams already using Fractal elsewhere will face minimal learning curve.vendor:publish for customization (e.g., default transformers, serializer settings). Auto-registers in Laravel 5.5+, reducing boilerplate.draw, columns, order, search) while adding Fractal’s include/fields for dynamic data shaping.league/fractal (~1MB) and yajra/laravel-datatables (~5MB). Low risk for most projects, but may conflict with existing Fractal versions or DataTables configurations.include/exclude logic. Teams new to transformers may need 1–2 days of ramp-up for nested relationships.include/fields parameters must be explicitly enabled in DataTables queries. Misconfiguration could lead to over-fetching or security risks (e.g., exposing sensitive fields).yajra/laravel-datatables-fractal for compatibility announcements.include parameters). May require additional test coverage.yajra/laravel-datatables?
Resource classes may suffice. For nested relationships or JSON:API compliance, this package is ideal.include/fields parameters are needed (e.g., for admin dashboards with role-based access), this package is a must. Otherwise, consider lighter-weight solutions.config/datatables.php and config/fractal.php for customizations that might clash with the package’s defaults.react-data-grid, ag-grid) via standardized JSON responses.relay-compiler) or REST APIs, reducing proxy complexity.yajra/laravel-datatables is installed (composer require yajra/laravel-datatables if missing).composer require yajra/laravel-datatables-fractal:^12.0
Yajra\DataTables\FractalServiceProvider in config/app.php.php artisan vendor:publish --tag=datatables-fractal
config/datatables-fractal.php for default transformers or serializer settings.app/Transformers/UserTransformer.php):
use League\Fractal\TransformerAbstract;
class UserTransformer extends TransformerAbstract {
public function transform(User $user) {
return [
'id' => $user->id,
'name' => $user->name,
'email' => $user->email,
'posts' => $user->posts->count(), // Example computed field
];
}
}
return DataTables::of(User::query())->make(true); with:
return DataTables::of(User::query())
->transformer(new UserTransformer)
->make(true);
return DataTables::of(User::query())
->addInclude(['posts', 'roles'])
->addField('users', ['id', 'name'])
->transformer(new UserTransformer)
->make(true);
data objects, meta for pagination).react-data-grid:
const columns = [
{ key: 'id', name: 'ID' },
{ key: 'name', name: 'Name' },
{ key: 'posts', name: 'Post Count' },
];
const data = response.data.data; // Access Fractal-transformed data
draw, columns, order, and search parameters are preserved in transformed responses.config/datatables.php (e.g., custom column mappings) that might conflict with Fractal’s response structure.ajax.dataSrc = 'data.data' in client-side DataTables to handle nested responses.spatie/laravel-fractal or laravel-nova if used elsewhere in the stack.ArraySerializer.admin/users) to test integration.User, Order, Product).include/fields parameters for endpoints requiring flexible data fetching.How can I help you explore Laravel packages today?