Product main image and files



Below query will return all information on the files / main images for all products (in the included()->main_images and included()->files array).
$response = $moltin->products->with(['files, main_images'])->all();

On a product level, I would expect that below query would also return all information on the main image and files for a given product ID
$response=$moltin->products->with(['files, main_images'])->get($productID);

The query, however, only returns the IDs and type (example below).

[relationships] => stdClass Object
            [files] => stdClass Object
                    [data] => Array
                            [0] => stdClass Object
                                    [type] => file
                                    [id] => 2c1fda42-d44a-44f9-a852-8504344f9096
                            [1] => stdClass Object
                                    [type] => file
                                    [id] => 134470ea-a744-4028-9c7e-0b8be1869f96
            [main_image] => stdClass Object
                    [data] => stdClass Object
                            [type] => main_image
                            [id] => e42b280f-b46d-4f08-850c-5beaf274d964

Is there a way to get all information on the files & main images for one product ID in one query? I try to avoid using the query which returns all products and then loop through the main_images / files array.


Hey @andre. There was a similar discussion around this here. You’ll need to use the $response->included() data to match up to your products. Hope that helps!


Thanks @andrew. What´s best practice to get images & files for just one product in the Moltin API:

A) Query all products incl main images & files (and loop through data to find matching IDs of above output)


B) Query all file IDs of above output individually with

$filesResponse = $moltin->files->get($fileID);

Is there a way with option B to include an array instead of only one file ID string?


I don’t think A or B are the best options.

Instead, if you know the product ID, then it would make much more sense (in terms of speed and an optimised number of calls) to do:

$moltin->products->with(['files', 'main_images'])->get($productID)

The response you get back will then have the included resources.


Thanks, this works like charm. Key was changing the single quotation marks form ['files, main_images'] to ['files', 'main_images']


This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.