2

This is a single asset field to upload multiple images so how to get all images using the query. enter image description here

I have three images here but I am getting one image using this code

$home = \craft\elements\Entry::find()
  ->section($section)->site(Craft::$app->request->getQueryParam('lang'))
  ->one();
  
  foreach ($home->infographics as $key => $infographicsValue) {
        $infographicsImage[$key]['image'] = $infographicsValue->infographicsImage;
    }

Output

{"results":[{"data":{"infographicsImage":[{"image":{"dimensions":{"width":"374","height":"370"},"alt":"Dyk3","copyright":null,"url":"https://lenz-dev-asset.sgp1.digitaloceanspaces.com/ecovero/dyk3.jpg"}}]}}]}

Please help me to solve this issue. Thank you!!

1
  • Can you add more of your code? The output looks like you are using Element API.
    – Oli
    Nov 16 at 6:53
1

Edit: Previous answer was incorrect, see update below.

What kind of field is infographics? How is it related to the infographicsImage field you're accessing in the loop?

Either way, one of those is the assets field. Accessing an Assets field by handle returns an AssetQuery, not an Asset itself. To access the resulting assets, you need to execute the query using ->one() or ->all(), which will give you either a single Asset object (or null) or an array of Asset objects (which may be empty).

Please provide more info regarding the fields.

2
  • I can't find the field 'infographics' of a particular section using the craft\elements\Asset Nov 16 at 5:21
  • @SantanuAdak My answer was incorrect, I've misread the code in your question. Either way, you're not executing the asset query correctly, see my updated answer. If you provide more information, I can provide a more detailed answer.
    – MoritzLost
    Nov 16 at 10:18
1

I have fixed this issue with this code

foreach ($home->infographics as $key => $infographicsValue) {
      
     $imageArr['width'] = $infographicsValue['width'] ? $infographicsValue['width'] : "";
     $imageArr['height'] = $infographicsValue['height'] ? $infographicsValue['height'] : "";
     $imageArr['alt'] = $infographicsValue['title'] ? $infographicsValue['title'] : "";
     $imageArr['url'] = $infographicsValue['url'] ? $infographicsValue['url'] : "";
      
     $infographicsImage[$key]['image'] = $imageArr;
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.