Custom layout - setting data from $get throws fatal Typed property initialization error

Flare error is here https://flareapp.io/share/LPd8ElvP I am creating a quote worksheet with a Custom layout component:
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\Component;

class QuoteSummary extends Component
{
protected string $view = 'forms.components.quote-summary';
public array $viewData = [];


public static function make(): static
{
return app(static::class);
}

public function viewData(array | \Closure $viewData): static
{
$this->viewData = $this->evaluate($viewData);
return $this;
}

public function getViewData(): array
{
return $this->viewData;
}
}
<?php

namespace App\Forms\Components;

use Filament\Forms\Components\Component;

class QuoteSummary extends Component
{
protected string $view = 'forms.components.quote-summary';
public array $viewData = [];


public static function make(): static
{
return app(static::class);
}

public function viewData(array | \Closure $viewData): static
{
$this->viewData = $this->evaluate($viewData);
return $this;
}

public function getViewData(): array
{
return $this->viewData;
}
}
and i'm trying to pass into the ->viewData method calculated values from other fields on the form
QuoteSummary::make()
->viewData(function (GET $get):array {
//return [];
$visits = $get('no_of_return_trips') * 2;
$distance_cost = $get('distance') * $visits * $get('distance_cost_per_km');
$driving_cost = $get('driving_time') / 60 * $get('driving_time_cost_per_hour');
$tolls = $get('tolls');
$parking = $get('parking_fees');
$van_hire = $get('requires_van_hire') ? $get('van_hire_charge') : 0;
$travel = $distance_cost + $driving_cost + $tolls + $parking + $van_hire;
$accommodation = $get('requires_accommodation') ? $get('accomm_no_of_staff') * $get('accomm_no_of_nights') * $get('accommodation_cost_per_person_per_night') : 0;

$return = [
'Travel' => '€' . number_format($travel, 2),
'Accommodation' => '€' . number_format($accommodation, 2),
'Catering' => '€0.00',
'Equipment' => '€0.00',
'Service Team' => '€0.00',
'Additional charges' => '€0.00',
];
return $return;
}),
QuoteSummary::make()
->viewData(function (GET $get):array {
//return [];
$visits = $get('no_of_return_trips') * 2;
$distance_cost = $get('distance') * $visits * $get('distance_cost_per_km');
$driving_cost = $get('driving_time') / 60 * $get('driving_time_cost_per_hour');
$tolls = $get('tolls');
$parking = $get('parking_fees');
$van_hire = $get('requires_van_hire') ? $get('van_hire_charge') : 0;
$travel = $distance_cost + $driving_cost + $tolls + $parking + $van_hire;
$accommodation = $get('requires_accommodation') ? $get('accomm_no_of_staff') * $get('accomm_no_of_nights') * $get('accommodation_cost_per_person_per_night') : 0;

$return = [
'Travel' => '€' . number_format($travel, 2),
'Accommodation' => '€' . number_format($accommodation, 2),
'Catering' => '€0.00',
'Equipment' => '€0.00',
'Service Team' => '€0.00',
'Additional charges' => '€0.00',
];
return $return;
}),
Thanks for any light that can shed on this
Flare
Typed property Filament\Forms\Components\Component::$container must not be accessed before initialization - The error occurred at https://f2t.test/admin/quotes/create
Solution:
I'm trying to use an existing method from the ViewComponent - if I change my method from viewData to layoutData and getLayoutData it's all good. Thanks for listening 🦆
Jump to solution
1 Reply
Solution
Blackpig
Blackpig4d ago
I'm trying to use an existing method from the ViewComponent - if I change my method from viewData to layoutData and getLayoutData it's all good. Thanks for listening 🦆