Content Migration: Unable to move files from Structure Field to Pages

I’m trying to programmatically migrate some content from a Structure Field to Pages. Everything works, except the migration of files.

Each entry in the Structure has a few fields, including one called receipts of type files that stores a single PDF file. The new page has the same blueprint structure.

The data of the structure as array / yaml looks more or less like this when dumped:

[{ ["title"]=> string(8) "Some text", ["receipt"]=> array(1) { [0]=> string(13) "some-file.pdf" } }]

I’m using createChild() to create pages.

I move existing files to the new page.

All pages are created, including their receipts with all data, except the actual filename.

// Loop through structure

foreach($structure as $entry) {
    
    // Create a page for each entry in the structure (works!)

    $page = page("...")->createChild([
        "template" => "...",
        "slug" => "...",
        "content" => [
          ...,
          "subscriptions" => $entry->subscriptions()->toArray()
         ]
    ]);

    // Move files from the old structure (with a nested structure field 
    // called "subscriptions") to the new page’s structure field (works!)

    foreach ($entry->subscriptions()->toStructure() as $subscription) {
        $file = $subscription->receipt()->first()->toFile();
        if ($file) {
            $file->copy($page);
        }
    } 
}

So all pages are created and the files are moved. But: the file path is not saved in the structure field. The files field is an empty array.

Subscriptions:
- 
  title: Some Text
  receipt: [ ]

Any idea what could be wrong?

My assumption: if the file doesn’t exist yet in the page folder, setting / selecting the file is not allowed / rejected. I now tried to:

  1. Create new page with basic data
  2. Move the files to the new page’s folder
  3. Re-write the data for the files

This then works. I wonder if there is a way to force the writing of the files reference even if they don’t exist yet? Or what would be the best order to create pages with files?