larasponse
larasponse copied to clipboard
responses should be capable of dealing with multiple models
For the sake of saving on HTTP requests, I tend to combine multiple GETs into one. E.g. (simplified):
<?php
class OrderInfoController extends BaseController
{
public function show()
{
$productsRequest = Request::create('/api/v1/products', 'GET');
$products = Route::dispatch($productsRequest)->getData();
$timesRequest = Request::create('/api/v1/deliverytimes', 'GET');
$times = Route::dispatch($timesRequest)->getData();
return Response::json(array(
'products' => $products,
'times' => $times
), 200);
}
}
Now, how can I deal with this situation using Larasponse?
I don't know if it's the proper way of doing it, but it works for me ....
getData(); $timesRequest = Request::create('/api/v1/deliverytimes', 'GET'); $times = Route::dispatch($timesRequest)->getData(); $products = $this->response->collection($products, new ProductsTransformer(), 'products'); $times = $this->response->collection($times, new TimesTransformer(), 'times'); // return array_merge($products, $times); return Response::json(array_merge($products, $times), 200); ?>
Any note on if this is a proper way?