title: How do you convert third party API data to a collection resource in Laravel 5.6?
body: <p>I've been working on creating a clean interface for our various web application and I've run into a snag with Laravel's API Resources not properly converting the incoming json array into laravel collections. </p>
  
  <p>I can do it with a single resource: </p>
  
  <pre><code>    namespace App\Http\Resources;
  
  use Illuminate\Http\Resources\Json\Resource;
  use App\Product;
  
  class ProductResource extends Resource
  {
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function toArray($request)
      {
          return [
              'name' =&gt; $this-&gt;resource['product']['name'],
              'description' =&gt; $this-&gt;resource['product']['description'],
              'price' =&gt; $this-&gt;resource['product']['rental_rate']['price']
          ];
  
          //return parent::toArray($request);
      }
  }
  </code></pre>
  
  <p>print this response outputs: </p>
  
  <pre><code>{"name":"Arri Skypanel S60-C","description":"Arri Sky Panel S60-C 450w input with a 2000w tungsten equivalent &amp; Combo Stand","price":"260.0"}
  </code></pre>
  
  <p>However trying to take this single item and turn it into a collection of items isn't going anywhere. </p>
  
  <p>Anybody got a clue what I'm missing?</p>
  
  <p>Pulling the API data looks like this: </p>
  
  <pre><code>namespace App;
  use GuzzleHttp\Exception\GuzzleException;
  use GuzzleHttp\Client;
  
  class ThirPartyAPI
  {
      private $url = 'https://api.third-party.com/api/v1/';
  
      public function pull($query, $additionalParams) {
          $client = new Client;
          $result = $client-&gt;get($this-&gt;url . $query . $additionalParams, [
              'headers' =&gt; [
                  'Content-Type' =&gt; 'application/json',
                  'X-AUTH-TOKEN' =&gt; env('CURRENT-AUTH-TOKEN'),
                  'X-SUBDOMAIN' =&gt; env('CURRENT-SUBDOMAIN')
              ]
          ]);
          $array = json_decode($result-&gt;getBody()-&gt;getContents(), true);
  
          return $array;
      }
  }
  </code></pre>
  
  <p>The API returns a lot of json data.</p>
  
  <p>This is the Product model: </p>
  
  <pre><code>public function getAllProducts() {
          try {
  
              $productData = [];  
                  $query = "/products?page=1&amp;per_page=3&amp;filtermode=active";
                  $additionalParams = "";
                  $productData = new ThirdPartyAPI;
                  $productData = $productData-&gt;pull($query, $additionalParams);
  
                  $productData = $productData['products'];    
  
          return ProductsResource::make($productData);
      } catch (\Exception $ex) {
          return $ex;
      } catch (\Throwable $ex) {
          return $ex;
      }
  
  } 
  </code></pre>
  
  <p>Right now I'm trying something this to convert all the returned arrays into something I can control more: </p>
  
  <pre><code>namespace App\Http\Resources;
  
  use Illuminate\Http\Resources\Json\Resource;
  
  class ProductsResource extends Resource
  {
      /**
       * Transform the resource into an array.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return array
       */
      public function toArray($request)
      {
          return [
              'products' =&gt; $this-&gt;collection-&gt;mapInto(function($request) {
                 return[ 'name' =&gt; $this-&gt;resource['name'],
                         'description' =&gt; $this-&gt;resource['description'],
                         'price' =&gt; $this-&gt;resource['rental_rate']['price']
                  ];
              })
          ];
  
      }
  </code></pre>
  
  <p>However var_dumping the data just returns this: </p>
  
  <pre><code>object(App\Http\Resources\ProductsResource)[200]
    public 'resource' =&gt; 
      array (size=3)
        0 =&gt; 
          array (size=37)
            'id' =&gt; int 164
            'name' =&gt; string '10A Dimmer' (length=10)
            [Lots of data]
                ...
            'sale_rates' =&gt; 
              array (size=0)
                ...
        1 =&gt; .....
        [cont]
   public 'with' =&gt; 
      array (size=0)
        empty
    public 'additional' =&gt; 
      array (size=0)
        empty
  </code></pre>
  
  <p>I've tried various forms of data conversion on the return json info and haven't had a lot of results except errors and confusing business. I'm a little shady on how Laravel handles API Resource handling. </p>