sixteensquared
sixteensquared
FFilament
Created by sixteensquared on 12/1/2023 in #❓┊help
Repeater Data in mutateFormDataBeforeCreate() method
I have a resource SalesOrderResource that implements a Repeater field (lines) to manage the sales order's line items. When the user clicks Create to add the model to the database I'm trying to modify some of the data before inserting it. In this case I would like to calculate the totals for the sales order by adding up the price for each line item. Easy enough, except that the $data parameter passed to the mutateFormDataBeforeCreate() method does not include the array of line items added via the repeater field, it only includes the fields for the sales order model. Am I going about this the right way? If so, what am I missing, if not, what is the best approach to calculate the order total before inserting it into the database?
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['number'] = SalesOrder::generateNumber();

foreach($data['lines'] as $line) {
$data['subtotal'] += number_format($line['subtotal'], 2);
$data['discounts'] += number_format($line['subtotal'] * ($line['discount']/100), 2);
$data['tax'] += number_format($line['tax'], 2);
$data['total'] += number_format($line['total'], 2);
}

return $data;
}
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['number'] = SalesOrder::generateNumber();

foreach($data['lines'] as $line) {
$data['subtotal'] += number_format($line['subtotal'], 2);
$data['discounts'] += number_format($line['subtotal'] * ($line['discount']/100), 2);
$data['tax'] += number_format($line['tax'], 2);
$data['total'] += number_format($line['total'], 2);
}

return $data;
}
Noting the block above, the $data['lines'] is not accessible. Here is the dump of what is included in the $data array.
array:14 [▼ // app/Filament/Resources/SalesOrderResource/Pages/CreateSalesOrder.php:16
"number" => null
"status" => "draft"
"customer_id" => "3"
"market_id" => 1
"account_manager_id" => 1
"price_list_id" => 1
"reference" => "12345"
"ship_date" => "2023-12-20"
"in_hands_date" => "2023-12-13"
"subtotal" => 0
"shipping" => 0
"discount" => 0
"tax" => 0
"total" => 0
]
array:14 [▼ // app/Filament/Resources/SalesOrderResource/Pages/CreateSalesOrder.php:16
"number" => null
"status" => "draft"
"customer_id" => "3"
"market_id" => 1
"account_manager_id" => 1
"price_list_id" => 1
"reference" => "12345"
"ship_date" => "2023-12-20"
"in_hands_date" => "2023-12-13"
"subtotal" => 0
"shipping" => 0
"discount" => 0
"tax" => 0
"total" => 0
]
1 replies