Open miguelsmuller opened 8 years ago
You mean "data" key? Answered here: https://github.com/thephpleague/fractal/issues/142
Add the ArraySerializer to your manager:
$this->fractalManager->setSerializer(new ArraySerializer());
For Include collections just create your own serializer, which inherits from the ArraySerializer like jasonlewis described.
class MyArraySerializer extends League\Fractal\Serializer\ArraySerializer { public function collection($resourceKey, array $data) { return $data; } }
I think (?) @miguelsmuller means he wants to keep the outer "data" key (for example, when paginating a collection) but wants to remove any include "data" keys (for example, while also returning all included relation records).
If that's right – @miguelsmuller please correct me if I'm wrong – does anyone know how to accomplish this?
Yes @furey. Exactly. I need to keep the outer "data" key but remove any include "data" keys.
I don't have a solution for: without "getting to change the serializer." . But in your transform() method you can set the data manual:
`namespace App\Transformers;
use League\Fractal\TransformerAbstract; use App\Transformers\ItemsTransformer;
class MyTransformer extends TransformerAbstract {
protected $defaultIncludes = [
'items'
];
public function transform(Foo $foo)
{
return[
'data' => [
'id' => (int) $foo->id,
'date' => $foo->date->toDateTimeString()
]
];
}
public function includeItems(Foo $foo)
{
$items = $foo->items;
return $this->collection($items, new ItemsTransformer);
}
}`
And for your included items you create your own Serializer:
`namespace App\Serializers;
use League\Fractal\Serializer\ArraySerializer;
class NoDataKeySerializer extends ArraySerializer { public function collection($resourceKey, array $data) { return $data; } }`
But as I said, I don't know hot to solve this, without changing the serializer:
$fractal = new Manager(); $fractal->setSerializer(new NoDataKeySerializer());
This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 4 weeks if no further activity occurs. Thank you for your contributions.
I need to remove the item [date] when I do include, but not getting to change the serializer.
This is the function that does include roomType