0

I'm trying to convert a given API response in something easy to work with.

This is what I have so far:

Controller:

public function drive()
{
    $optParams = [
        'corpora' => 'drive',
        'driveId' => env('GOOGLE_DRIVE_ID'),
        'includeItemsFromAllDrives' => true,
        'supportsAllDrives' => true,
        'fields' => 'files(*)'
    ];

    $results = $this->googleDrive->files->listFiles($optParams);
    $data = collect($results);
    $collection = new FileCollection($data);
    dd($collection);

    return 'Hi!';
}

FileCollection resource:

public function toArray($request)
{
    return [
        'data' => $this->collection,
        'meta' => ['files_count' => $this->collection->count()],
    ];
}

File resource:

public function toArray($request)
{
    return [
        'name' => $this->resource['name'],
        'mime' => $this->resource['mimeType'],
        'parents' => $this->resource['parents'],
        'version' => $this->resource['version'],
        'webDownloadLink' => $this->resource['webContentLink'],
        'webLink' => $this->resource['webViewLink'],
        'modified_at' => $this->resource['modifiedTime'],
        'size' => $this->resource['size']
    ];
}

This is the result I'm getting:

FileCollection {#223 ▼
  +collects: null
  +collection: Collection {#243 ▶}
  +resource: Collection {#243 ▼
    #items: array:2 [▼
      0 => File {#224 ▼
        +resource: Google_Service_Drive_DriveFile {#279 ▶}
        +with: []
        +additional: []
      }
      1 => File {#263 ▼
        +resource: Google_Service_Drive_DriveFile {#269 ▶}
        +with: []
        +additional: []
      }
    ]
  }
  +with: []
  +additional: []
}

So as you can see the types of resources are correct, but for some reason the 'meta' in the FileCollection isn't there, nor are the fields in the File resource. All fields are still shown, like 30+ (instead of the 8 requested in the FileResource).

Am I missing something here?


EDIT:

When accessing the collection with the toArray() method ($collection->toArray(null)), I do indeed get the appropriate result:

array:2 [▼
  "data" => Collection {#243 ▼
    #items: array:2 [▼
      0 => File {#224 ▶}
      1 => File {#263 ▶}
    ]
  }
  "meta" => array:1 [▼
    "files_count" => 2
  ]
]

So how do I make sure now that this collection uses the toArray() of the FileResources? (I'm still getting over 30 fields, instead of 8).

PS: I don't really get why I need to call the toArray(), nowhere in their documentation is it written : https://laravel.com/docs/5.8/eloquent-resources .

Goowik
  • 782
  • 11
  • 36
  • You are really missing a lot of code here, and maybe have posted some things that aren't necessary. Please try to post all (and only) relevant code, see https://stackoverflow.com/help/minimal-reproducible-example. From looking at this, it simply looks like you dump your ```FileCollection``` class and not the result of ```toArray```? – Josh May 23 '19 at 18:27
  • @Josh Thanks for your reply, I edited my post. To be honest except for the class definitions I didn't write, there is no other code. It's a clean Laravel install. Well you have a GoogleDriveServiceProvider, but that just returns a service in the controller (`$this->googleDrive`) – Goowik May 24 '19 at 08:32
  • my mistake, I didn't know that a resource was a feature of Laravel. Looking at the documentation you seem to be doing the right thing, maybe it only works for Eloquent models? The document does repeatedly refer to Eloquent models and is under the Eloquent heading. – Josh May 24 '19 at 11:45

0 Answers0