To save deep relationships like this, you need to create the WorksheetItems and WorksheetItemsMonths individually and then associate them with each other. Here's an example of how you can do it:
// Create a new Worksheet
$worksheet = new Worksheet([
'name' => 'Some Worksheet',
]);
// Save the Worksheet
$worksheet->save();
// Create a new WorksheetItem
$worksheetItem = new WorksheetItem([
'name' => 'Some Name',
]);
// Associate the WorksheetItem with the Worksheet
$worksheet->items()->save($worksheetItem);
// Create new WorksheetItemMonths
$worksheetItemMonth1 = new WorksheetItemMonth([
'period' => '2023-05-27',
'amount' => 10.00,
]);
$worksheetItemMonth2 = new WorksheetItemMonth([
'period' => '2023-06-27',
'amount' => 10.00,
]);
// Associate the WorksheetItemMonths with the WorksheetItem
$worksheetItem->months()->saveMany([$worksheetItemMonth1, $worksheetItemMonth2]);
In this example, we first create a new Worksheet and save it. Then we create a new WorksheetItem and associate it with the Worksheet using the save method on the items relationship. Finally, we create two new WorksheetItemMonths and associate them with the WorksheetItem using the saveMany method on the months relationship.
Note that you should replace the model class names (Worksheet, WorksheetItem, WorksheetItemMonth) with the actual names of your models.
