createFile() in child page

Hello everyone,

I have created a controller with which I can create a page via the frontend on the one hand and upload and create a file on the other. Both work without any problems.

But now I want the uploaded file not to be created in the current page, but in the one I just created. At the end, a child page should be created in which a PDF file of the same name is stored.

Here is my previous code snippet, in which the file is only ever stored in the parent page, not in the child page. I am at a bit of a loss as to why this is the case.

try {

        $name = crc32($upload['name'].microtime()). '_' . $upload['name'];

        $page->createChild([
          'slug' => $name,
          'draft' => false,
          'template' => 'event',
        ]);

        $file = $page->createFile([
          'source'   => $upload['tmp_name'],
          'filename' => $name,
          'template' => 'upload',
          'parent' => $page->children()->find($name),
          'content' => [
              'date' => date('Y-m-d h:m')
          ]
        ]);

        $success = 'Your file upload was successful';



      }

Many thanks for your help!

$page refers to the current page, not the child.

And in any case, object in Kirby are immutable, therefore you need to store the result of an action in a new variable:

$childPage = $page->createChild([
          'slug' => $name,
          'draft' => false,
          'template' => 'event',
        ]);

Then

    $file = $childPage->createFile([
          'source'   => $upload['tmp_name'],
          'filename' => $name,
          'template' => 'upload',
          'parent' => $page->children()->find($name),
          'content' => [
              'date' => date('Y-m-d h:m')
          ]
        ]);