larasponse icon indicating copy to clipboard operation
larasponse copied to clipboard

responses should be capable of dealing with multiple models

Open tommedema opened this issue 10 years ago • 2 comments

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?

tommedema avatar Oct 23 '14 12:10 tommedema

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); ?>

SeoRoman avatar May 26 '15 21:05 SeoRoman

Any note on if this is a proper way?

tommedema avatar May 27 '15 07:05 tommedema