Creating page inside a loop

Hi! I’m trying to create some pages inside a loop. But when I try to create a page that is a child page of just created page it seems that is not possible and it creates the child page in the content root directory. Below is the code, there you can see that the signup page is child to account. There is any some kind of function that updates the site pages index? Or a better way to achieve this?

Thanks in advance.

    $pagesToCreate = [
        ['uri' => 'login', 'slug' => 'login', 'template' => 'login', 'status' => 'unlisted'],
        ['uri' => 'account', 'slug' => 'account', 'template' => 'account', 'status' => 'unlisted'],
        ['uri' => 'account/signup', 'slug' => 'signup', 'template' => 'signup', 'status' => 'unlisted']
    ];
    
    foreach ($pagesToCreate as $page) {
        $uri = $page['uri'];
        if (page($uri)) {
            array_push($returnMessage, 'WARN <b>'. $uri .'</b> is already present' );
        } else {
            if (strpos($uri, '/')) {
                $parentPage = substr($uri, 0,strrpos($uri, '/'));
            } else {
                $parentPage = null;
            };

            $newPage = Page::create([
                'parent'   => page($parentPage),
                'slug'     => $page['slug'],
                'template' => $page['template']
            ]);
            $newPage->changeStatus($page['status']);
           
            array_push($returnMessage, 'OK <b>'. $uri .'</b> created.' );
        }
    };

Try $parentPage->createChild([]);

Hi! Already tried this, with no luck :frowning:
Thanks!

The problem here is that you try to create children of a just created page without fetching this page first, i.e. addressing it via the $newPage variable.

Hmm I thought I was doing that whit this page($parentPage). I’m not sure how to solve this? Can you please give any other hint?

What is the purpose of your script? Does the set of pages to create contain pages on all sorts of levels? I think things would be easier if the children of a page like the account page were actually part of that array with a children key, then you could check for that key and use the newly created page to create the children, something like:

$pagesToCreate = [
  ['uri' => 'login', 'slug' => 'login', 'template' => 'login', 'status' => 'unlisted'],
  ['uri' => 'account', 'slug' => 'account', 'template' => 'account', 'status' => 'unlisted', 'children' => [

    ['uri' => 'signup', 'slug' => 'signup', 'template' => 'signup', 'status' => 'unlisted']
  ]],
];

$returnMessage = [];
foreach ($pagesToCreate as $page) {
  $uri = $page['uri'];
  if (page($uri)) {
      array_push($returnMessage, 'WARN <b>'. $uri .'</b> is already present' );
  } else {
    
      $newPage = $site->createChild([
          'slug'     => $page['slug'],
          'template' => $page['template']
      ]);
      if (isset($page['children'])) {
        dump($page['children']);
        foreach ($page['children'] as $child) {
          $newChild = $newPage->createChild([
            'slug'     => $child['slug'],
            'template' => $child['template']
          ]);
          $newChild->changeStatus($child['status']);
        }
      }
      $newPage->changeStatus($page['status']);
     
      array_push($returnMessage, 'OK <b>'. $uri .'</b> created.' );
  }
};

The purpose is to make a like a “quick install” . The solution you suggested worked perfectly for me. Once again, thank you for your help. You are amazing!