- How do I install yajra/laravel-datatables-fractal in a Laravel 12 project?
- Run `composer require yajra/laravel-datatables-fractal:^12.0` to install the package. Laravel 12+ auto-discovers the service provider, so no manual registration is needed unless you’re using an older version. Ensure you also have `yajra/laravel-datatables` installed as a dependency.
- Can I use this package without yajra/laravel-datatables?
- No, this package is a plugin for `yajra/laravel-datatables` and requires it to function. If you’re not already using DataTables for server-side processing, consider alternatives like Laravel’s built-in API Resources or Spatie’s Laravel Query Builder.
- What’s the difference between this and Laravel’s built-in API Resources?
- This package integrates Fractal transformers directly with DataTables’ server-side processing, enabling dynamic `include` and `fields` parameters for granular data control. API Resources lack this flexibility for DataTables-specific queries, making this ideal for admin dashboards or SPAs needing dynamic table responses.
- How do I configure custom transformers for DataTables responses?
- Publish the config with `php artisan vendor:publish --tag=datatables-fractal`, then define transformers in the `app/Transformers` directory. Use Fractal’s `ItemTransformer` or `CollectionTransformer` to shape your DataTables query results. Example: `return fractal($query->get(), new UserTransformer());` in your DataTables controller.
- Does this package support Laravel 13?
- Yes, the package supports Laravel 13 via the `^13.0` version tag. Check the [compatibility table](https://github.com/yajra/laravel-datatables-fractal#laravel-version-compatibility) for exact version mappings. Always update dependencies to avoid breaking changes.
- How do I handle nested relationships with include/fields parameters?
- Enable dynamic includes by adding `$this->app->make('datatables')->include('relationships')` to your DataTables query. Use Fractal’s `include` method in transformers to load nested data. Example: `?include=orders&fields[users]=id,name` fetches only specified fields for related models.
- Will this package slow down my DataTables queries?
- No, it inherits DataTables’ efficient server-side processing and Fractal’s lazy serialization. However, improperly configured `include` or `fields` parameters can cause over-fetching. Test with `DB::enableQueryLog()` to ensure no N+1 queries occur in production.
- Can I use this with Laravel’s API Resources instead of Fractal?
- No, this package is designed for Fractal transformers and won’t work with API Resources. If you prefer API Resources, consider using `yajra/laravel-datatables` alone or extending it with custom JSON serialization logic in your controllers.
- How do I test DataTables responses with Fractal transformers?
- Mock the DataTables query builder and Fractal’s `Manager` in your tests. Use `DataTables::of(Model::class)` to simulate server-side processing, then assert the transformed JSON structure. Example: `$response->assertJsonStructure(['data' => ['*' => ['id', 'name']]]).`
- What’s the best way to migrate existing DataTables endpoints to use Fractal?
- Start by wrapping your DataTables query in a Fractal transformer in the controller. Gradually refactor high-traffic endpoints first. Use `php artisan vendor:publish --tag=datatables-fractal` to customize default settings. Test each endpoint with tools like Postman to verify the new JSON structure.