Accessing structure content

I have the following blueprint…

title: Homepage
columns:
    - width: 1/2
        sections:
            books:
                type: fields
                fields:
                    book1:
                        label: BOOK 1
                        type: headline
                        numbered: false
                    sectionheading1:
                        label: Heading of Section (optional)
                        type: text
                    booktitle1:
                        label: Title of First Book
                        type: text
                    bookcover1:
                        label: Image of First Book Cover
                        type: files
                        layout: cards
                        max: 1
                    bookdesc1:
                        label: First Book Description
                        type: textarea
                    line:
                        type: line
                    book2:
                        label: BOOK 2
                        type: headline
                        numbered: false
                    sectionheading2:
                        label: Heading of Section (optional)
                        type: text
                    booktitle2:
                        label: Title of Second Book
                        type: text
                    bookcover2:
                        label: Image of Second Book Cover
                        type: files
                        layout: cards
                        max: 1
                    bookdesc2:
                        label: Second Book Description
                        type: textarea
                        
    - width: 1/2
        sections:
            reviews:
                type: fields
                fields:
                    review:
                        label: Customer Reviews
                        type: structure
                        fields:
                            bookname: 
                                label: Book Name
                                type: select
                                options: 
                                    theforgotten: The Forgotten
                                    takingroot: Taking Root
                            bookreview:
                                label: Review
                                type: textarea
                            author:
                                label: Author of Review
                                type: text

I am trying to print out a random review but cannot seem to get any meaningful result.

In my last attempt I tried to print out all the reviews with the code below but with no success. Can anyone advise please.

 <?php foreach ($page->reviews()->review()->toStructure() as $review): ?>
<?php echo review->bookreview() ?>
<?php endforeach ?>

Couple of mistakes there i think missing $ and accessing the field incorrectly (you dont need to go through the section to get to it)

 <?php foreach ($page->review()->toStructure() as $review): ?>
<?php echo $review->bookreview() ?>
<?php endforeach ?>

For the random reveiwe i think you should be able to use shuffle and then grab the first one

<?php foreach ($page->review()->toStructure()->shuffle()->first() as $review): ?>
<?php echo $review->bookreview() ?>
<?php endforeach ?>

Not sure if i have ever tried this but i think you can probably shorten and do away with the loop, since you only want one anyway:

<?php $random = $page->review()->toStructure()->shuffle()->first() ?>
<?php echo $random->bookreview() ?>

Thank you, that worked for the shuffle but I get no output when I use first()

This should work, however, the code is error-prone and should be wrapped in an if statement:

<?php if($random = $page->review()->toStructure()->shuffle()->first()): ?>
<?php echo $random->bookreview() ?>
<?php endif; ?>

This loop with first() cannot possibly work, because a single item (StructureObject) is not iterable.

Woops yes of course… long day :zzz:

@Loudon This is the way to go…

<?php if($random = $page->review()->toStructure()->shuffle()->first()): ?>
<?php echo $random->bookreview() ?>
<?php endif; ?>

Brilliant, thank you. I’ve been staring at this for hours and so relieved it is now working perfectly.