I want to use laravel-datatables-fractal for transforming server-side response using Fractal.
First I wrote a transformer for Product
model like this :
class ProductTransformer extends TransformerAbstract
{
protected $availableIncludes = [
'prices'
];
public function transform(Product $product)
{
$transforms = [
'product_id' => $product->product_id,
'code' => $product->code,
'title' => $product->title,
'description' => $product->description,
];
return $transforms;
}
public function includePrices(Product $product)
{
$prices = $product->prices;
return $this->collection($prices, new PriceTransformer, FALSE);
}
}
As you can see there is an included prices
attribute that uses another transformer named PriceTransformer
.
And in the ProductController
that makes and returns result of all products as a datatable formatted response I have a datatable
method like this:
public function datatable(Request $request)
{
return app('datatables')->of(Product::all())
->setTransformer(new ProductTransformer)
// ->setSerializer(new CustomArraySerializer())
->make(TRUE);
}
Response is returned but a problem that I have is : when I want to return prices
included attribute , it returned as a data
attribute like this :
{
"draw": 0,
"recordsTotal": 8,
"recordsFiltered": 8,
"data": [
{
"product_id": 1,
"title": "Product Title 1",
"for_sale": true,
"for_purchase": true,
"changeable": false,
"returnable": false,
"sale_price": "40000.00",
"purchase_price": "5000.00",
"creator": 1,
"created_at": "2017-12-11 12:21:49",
"updated_at": "2017-12-14 11:55:52",
"prices": {
"data": [
{
"sale_price": "30000.00",
"created_at": "2017-12-11 12:21:49"
},
{
"sale_price": "40000.00",
"created_at": "2017-12-11 12:39:00"
},
]
}
},
{
"product_id": 11,
"title": "Product Title 11",
"for_sale": true,
"for_purchase": true,
"changeable": false,
"returnable": false,
"sale_price": "50000.00",
"purchase_price": "40000.00",
"creator": 1,
"created_at": "2017-12-16 11:07:43",
"updated_at": "2017-12-16 11:07:43",
"prices": {
"data": [
{
"sale_price": "50000.00",
"created_at": "2017-12-16 11:07:43"
}
]
}
}
],
"input": {
"include": "prices"
}
}
I want to remove data
from included attributes and they be in this format for example :
"prices": [
{
"sale_price": "50000.00",
"created_at": "2017-12-16 11:07:43"
}
]
Even I added a serializer (as you see in above code and is commented) with below content that I used it for all other responses and worked well :
class CustomArraySerializer extends ArraySerializer
{
public function collection($resourceKey, array $data)
{
if ($resourceKey === FALSE) {
return $data;
}
return ['success' => TRUE, 'result' => $data];
}
public function item($resourceKey, array $data)
{
if ($resourceKey === FALSE) {
return $data;
}
return ['success' => TRUE, 'result' => $data];
}
}
But when use it I got this error :
{
"draw": 0,
"recordsTotal": 8,
"recordsFiltered": 0,
"data": [],
"error": "Exception Message:\n\nUndefined index: data"
}
What is problem and how can solve that ?
I am using Spatie/Transformer "spatie/laravel-fractal", try using following after adding above mentioned package.