Possible to use TextInputColumn to update a relationship that has pivot table?
6 Replies
In depth, i have a 'Product' table and another pivot table as 'Products_merchants'. In 'Products_merchants' table i will have a column called 'stock_count' to indicate how many stocks of the product that the merchant have.
1. When using TextInputColumn, when it onChange, noticed that it will call $wire.updateTableColumnState where it will grabs the key and id and the target value and update it. But however, this is only applies to Product Model but not products_merchants.
Is there anywhere to do so with TextInputColumn?
I'm curious, can people just change stock counts in the system, without any other kind of verification?
Hi thanks for replying. Whoever that able to login into the system, they are automatically able to edit the stock counts.
Solution
But i have resolved this by using updateStateUsing. Thanks again.
Thank you for pointing me in the direction of the updateStateUsing, i was able to update a column when i editted a column in the table.
👍
Just as an addition I have added a notification to each TextInputColumn
Tables\Columns\TextInputColumn::make('monday')
->rules(['required', 'numeric', 'between:0,24'])
->updateStateUsing(function (Model $record, $state) {
// Get the old Monday hours and calculate the difference
$oldHours = $record->monday;
$difference = $oldHours - $state;
// Update the 'monday' column and calculate the new contracted hours
$record->monday = $state;
$contractedDifference = $record->contracted_hours - $difference;
// Update the 'contracted_hours' column
$record->contracted_hours = $contractedDifference;
$record->save();
Notification::make()
->title('Updated successfully')
->icon('heroicon-o-face-smile')
->iconColor('success')
->duration(3000)
->send();
// Return the updated model instance
return $record;
}),