Foreach over where class CakePHP 4

I’m using collections to generate a menu of categories.
The idea is to have this structure:

<nav class="collapse show navbar navbar-vertical navbar-light align-items-start p-0 border border-top-0 border-bottom-0" id="navbar-vertical">
    <div class="navbar-nav w-100 overflow-hidden" style="height: 350px">
        <a href="" class="nav-item nav-link">Menu A</a>
        <a href="" class="nav-item nav-link">Menu B</a>
        <a href="" class="nav-item nav-link">Menu C</a>
    </div>
</nav>

Using collections, I managed to bring the categories in this way:

<nav class="collapse show navbar navbar-vertical navbar-light align-items-start p-0 border border-top-0 border-bottom-0" id="navbar-vertical">
    <div class="navbar-nav w-100 overflow-hidden" style="height: 350px">
        <?php echo $this->Html->link(collection($json["data"])->combine('id', 'name')->toArray(), '/pages/link', array('class' => 'nav-item nav-link ')); ?>
    </div>
</nav>

The problem is that the menus are not one under the other.

Could you help me with this structure?

There’s no foreach anywhere here. You’re calling the link function exactly once, which means you’ll get exactly one link, not one for each menu item. Can’t say exactly what the right code would be here, as it depends very much on the structure of your $json variable, which you haven’t shown.

As Zuluru said, there is no foreach and we dont know the $json variable.

<nav class="collapse show navbar navbar-vertical navbar-light align-items-start p-0 border border-top-0 border-bottom-0" id="navbar-vertical">
    <div class="navbar-nav w-100 overflow-hidden" style="height: 350px">
       <?php 
			$collections = collection($json["data"])->combine('id', 'name')->toArray();
			foreach($collections as $collection){
				echo $this->Html->link($collection, '/pages/link', array('class' => 'nav-item nav-link '));
			}
		?>
    </div>
</nav>

but it should look like this.

1 Like