Not reaching children stucture fields from parent page

Dear Kirbies

I am running into an “Call to a member function toStructure() on null” error trying to reach the child pages structure fields from the parent page.

This works fine on the child pages when pointing to the stucture fields called “cargo”:

$cargodata = $page->cargo()->toStructure();

If i try to get from its parent page like following i run into the error:

$cargochildren = $page->children()->cargo()->toStructure();

I tried the turnarounds with page(‘’) and ->blueprint() but that didn’t resolved it either.
Has somebody run into the same issue and solved it? If yes please give me a hint.
Thanks in advance for your precious help. Hugs.

$page->children() returns a Pages object (collection of pages), however, you can only ever call the cargo field on a single Page object, so you would have to loop through the pages, then get the structure field for each individual page inside the loop:

foreach ($page->children()->listed() as $child) {
   $cargodata = $child->cargo()->toStructure();
  // ...
}

I see. Loop first. Thanx for the explanation. Yeah, works perfectly. I love you. :smiling_face_with_three_hearts:

Ups, i am facing a new problem.

Inside the foreach loop im trying to count and sum values:

$cargocount = count($cargodata->pluck('cargoamount', ',')); 
$cargototal  = array_sum($cargodata->pluck('cargoamount', ','));

on the child page echoing $cargocount and $cargotal is working fine.
i var_dumped $cargototal on the parent page and it looks fine, showing the values.

int(0) int(0) int(50000) int(25000) int(0) int(0)

Heeeelp! What i am overlooking now?

From what you outline above, I’m not really sure what you want to achieve and what your result is vs the expected result. What are those numbers and in what way are they wrong?

And please post the blueprint definition for your structure field.

If you are inside the loop, the values will always reflect only the values of the corresponding child. If you want to add up all values of all children, you need a different approach.

It would also help if you post the complete code that is relevant to understand what you are doing.

Yes of course. Here is the blueprint of a shipowners ship. An it’s listing the multiple transportation deals made with that specific ship.

cargo:
	type: structure
	fields:

		cargoamount:
			label: Amount
			type: number
			before: $
								
		cargocharterer:						
			label: Charterer
			type: select
			options:
				type: query
				query: page.parent.children.filterBy('category', 'charterer')

		cargobroker:						
			label: Shipbroker
			type: select
			options:
				type: query
				query: page.parent.children.filterBy('category', 'broker')
									
		cargodate:
			label: Date
			type: date
			display: DD.MM.YYYY

On the single ship page there is a list of all deals, and two global totals. one is the total amount of all deals. and the other how many deals were made. Thank works perfectly with this:

$cargodata = $page->cargo()->toStructure();
$cargocount = count($cargodata->pluck('cargoamount', ','));
$cargototal = array_sum($cargodata->pluck('cargoamount', ','));
foreach ($cargodata as $cargo)

On the overview page of all ships (parent page) i want to do the same but for all ships together. So based on the working php on the single ship, i projected it for the overview page. whith your help the list of all deals of all ships is working on the overview:

$ships = $page->children()->listed();

				<?php foreach ($ships as $ship): { 
					$cargodata = $ship->cargo()->toStructure();
					}
				?>
					<?php foreach ($cargodata as $cargo): ?>
					<tr>
						<td>
							<?= $cargo->cargodate()->toDate('%Y-%m-%d') ?>
						</td>
						<td>
							<?php $cargoC = $cargo->cargocharterer()->toPage(); ?>
							<?= $cargoC->title()->h()->esc() ?>						
						</td>
						<td>
							<?php $cargoB = $cargo->cargobroker()->toPage(); ?>
							<?= $cargoB->title()->h()->esc() ?>
						</td>
						<td>
							<?php $cargoA = $cargo->cargoamount()->value(); ?>
							$<?php echo number_format( $cargoA, 0, '.', "'"); ?>
						</td>
					</tr>
					<?php endforeach ?>
				<?php endforeach ?>

I am struggeling now with the two totals of all deals. One is total amount of all deals made by all ships. And the other is the total amount of all deals by all ships. Following the code on the overview page of all ships:

$ships = $page->children()->listed();
foreach ($ships as $ship) { 
	$cargodata = $ship->cargo()->toStructure();	
	$cargocount = count($cargodata->pluck('cargoamount', ',')); 
	$cargototal = array_sum($cargodata->pluck('cargoamount', ','));
	var_dump($cargototal);
}

I put the count() and the array_sum() into the loop, but echoing them is not delivering the result, and i dont get an error message. And i dont get it. Thanx for your precious help. Hugs.

$ships = $page->children()->listed();
$cargoCount = 0;
$cargoTotal = 0;
foreach ($ships as $ship) { 
	$cargodata  = $ship->cargo()->toStructure();	
	$cargoCount += count($cargodata->pluck('cargoamount', ',')); 
	$cargoTotal += array_sum($cargodata->pluck('cargoamount', ','));

}

var_dump($cargoCount);
var_dump($cargoTotal);

If you want to add up the totals of each, you need to define the variables outside the loop and add to them inside the loop, then get the totals after the loop.

That’s it. Works perfectly. I tried something similar, but everything inside the loop, ich Dumpfbacke. Danke. :kissing_heart: