OK, so I’ve taken it down to the following two examples.
Example 1:
<ul id="cat">
<?php foreach($page->children()->visible()->sortBy('category', 'asc')->pluck('category') as $cat): ?>
<li><?php echo $cat->title()->html() ?></li>
<ul><?php foreach($page->children()->visible()->sortBy('title', 'asc') as $subcat): ?>
<?php if($subcat->category() == $cat->category()): ?>
<li><?php echo $subcat->title()->html() ?></li></ul>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
</ul>
Example 2:
<ul id="cat">
<?php foreach($page->children()->visible()->sortBy('category', 'asc')->pluck('category', null, true) as $cat): ?>
<li><?php echo $cat->title()->html() ?></li>
<ul><?php foreach($page->children()->visible()->sortBy('title', 'asc') as $subcat): ?>
<?php if($subcat->category() == $cat->category()): ?>
<li><?php echo $subcat->title()->html() ?></li></ul>
<?php endif ?>
<?php endforeach ?>
<?php endforeach ?>
</ul>
The key difference between the two is in the following statements on line 2, regarding pluck().
pluck('category')
pluck('category', null, true)
What I’ve found is that with the first example (the unique attribute is set to false), I get a menu that lists the category, then the first page with that category. It then lists the category again, with the second page with that category, etc.
So it the result looks something like this:
Fiction
Of Mice and Men
Fiction
Enders Game
Fiction
1984
Non-fiction
Dead Wake
Non-Fiction
Red Notice
With the second example (the unique attribute is set to true), I get this:
Fiction
Of Mice and Men
Non-fiction
Dead Wake
Is pluck() maybe not the best option here?