Access structure field data without foreach

Hi,

I’m currently working with structure fields, they’re awesome!

In this particular case I have max: 1, so only one row. Is there a way to access the field data without using foreach loop? I don’t need it since there’s just one row of data.

First I thought I could do something like this:

<?php if ($page->style()->background()->value() === 'dark'): ?>

But apparently not :pensive:

$item = $page->style()->toStructure()->first();
if ($item) {
  echo $item->background();
}

or whatever code you need here…

Note that if you don’t use an if statement to check if the item exists, you will get an error message if there is no entry.

1 Like

Wow awesome! I’m in love with the structure field, makes the UI so much cleaner.