1

I have a Super Table field with two columns, one is a category field, the other an assets field (limit 1 on both). How can I iterate through it to get the category and asset selected for each row in the table?

$superTable = $entry->colors;
foreach ($superTable as $row) {
    // Category field handle is `color`
    // Asset field handle is `image`

    // I've tried various things here, but I don't understand how to get the fields
    // I'm looking for the category ID and asset ID in every row of the table
}
1

Assuming your Super Table field has a handle of colors, you should be able to do:

foreach ($entry->colors as $block) {
    // Color will return a category element query, so you can do:

    // Return a collection of all category ids
    Craft::dump($block->color->ids());

    // Get the first category
    Craft::dump($block->color->one());

    // Get all the categories in the field
    Craft::dump($block->color->all());
}
| improve this answer | |

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.