F
Filament3w ago
Hedi

Repeater deletes the record instead of updating it

Hello, i have a repeater like this, the repeater is too long i'll just paste the essential lines
Forms\Components\Repeater::make('octaneStockProducts')
->dehydrated(true)
->relationship()
->live(onBlur: true)
->schema([
TextInput('quantity_price'),
TextInput('tax'),
TextInput('actual_quantity_price')
])
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get) use ($dollar_price) {
CheckinResource::calculateActualPrices($get, $set, $dollar_price);
})
Forms\Components\Repeater::make('octaneStockProducts')
->dehydrated(true)
->relationship()
->live(onBlur: true)
->schema([
TextInput('quantity_price'),
TextInput('tax'),
TextInput('actual_quantity_price')
])
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get) use ($dollar_price) {
CheckinResource::calculateActualPrices($get, $set, $dollar_price);
})
the problem occurs inside CheckinResource::calculateActualPrices, below is the code of this method
$stock_products = [];
foreach ($get('octaneStockProducts') as $index => $product) {
//don't mind the math here, this is just for the sake of the sample code
$product['actual_quantity_price']=$product['quantity_price']+$product['tax']

array_push($stock_products, $product);
}

$set('octaneStockProducts', $stock_products);
$stock_products = [];
foreach ($get('octaneStockProducts') as $index => $product) {
//don't mind the math here, this is just for the sake of the sample code
$product['actual_quantity_price']=$product['quantity_price']+$product['tax']

array_push($stock_products, $product);
}

$set('octaneStockProducts', $stock_products);
as you can see, i'm iterating through the repeater records, i calculate the actual_quantity_price, then add that changed record to a new list. then i assign that new list to the repeater. this causes the repeater to delete all the records and creating new ones, i know this is kinda expected to happen. but i was wondering, is there like a primary key (using the id inside $product variable) that i can assign to each repeater, so instead of deleting and creating, it updates the record based on the $product's id. thanks for any suggestions!
5 Replies
toeknee
toeknee3w ago
This is relationship type dependant. It deletes usually because it doesn't know which record should exist withoud an ID. What type of relationship are you using?
Hedi
HediOP2w ago
but the octaneStockProduct relationship has an id, this is my relationship
public function octaneStockProducts()
{
return $this->hasMany(OctaneStockProduct::class, 'octane_stock_id');
}
public function octaneStockProducts()
{
return $this->hasMany(OctaneStockProduct::class, 'octane_stock_id');
}
toeknee
toeknee2w ago
That’s fine, but if you look at the code I provided it included the id in the name which will return the integer opposed to returning the array of integers / values.
Hedi
HediOP2w ago
could you please elaborate? i don't get what you mean
Dennis Koch
Dennis Koch2w ago
How about? Just some guessing, but the key might be needed.
$stock_products[$index] = $product;
$stock_products[$index] = $product;

Did you find this page helpful?