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?