Get related Page by the value of a structurefield

$user = $site->user();
  if(!$user) go('/form');
  $user_name = $user->user_name();
  $related_pages = page('owner')->children();
  $related_page = $related_pages->filter(function($p) {
    $members = $p->members()->toStructure();
    $member_arr = [];

    global $user_name;
    foreach($members as $member) {
      $member_arr[] = $member->member()->value();
    }
    return in_array($user_name, $member_arr);
  });

This is not working because I cant pass a variable to a callback function: global $user_name is empty. Is there a more elegant way?

Yes, you can use use()`:

$related_page = $related_pages->filter(function($p) use($user_name) {
 // ...
}
1 Like

This is working :grinning:
After all learning by doing I have to dig deeper in a more structured way… :slightly_smiling_face: