Possible to use TextInputColumn to update a relationship that has pivot table?

TextInputColumn::make('stock_count')
->label('Stock Count')
->visible(request()->has('merchant_id'))
->default(function ($record) {
// at the moment we get the first variant
$variant_id = $record->variants()->first()->id;
// lazy load 2 levels.
$stocks = $record->variants()->where('id', $variant_id)->first()
->merchants()->where('merchant_store_id', request()->get('merchant_id'))->first();

return $stocks ? $stocks->pivot->stock_count : 0;

})->label('Stock Count')
TextInputColumn::make('stock_count')
->label('Stock Count')
->visible(request()->has('merchant_id'))
->default(function ($record) {
// at the moment we get the first variant
$variant_id = $record->variants()->first()->id;
// lazy load 2 levels.
$stocks = $record->variants()->where('id', $variant_id)->first()
->merchants()->where('merchant_store_id', request()->get('merchant_id'))->first();

return $stocks ? $stocks->pivot->stock_count : 0;

})->label('Stock Count')
Solution:
But i have resolved this by using updateStateUsing. Thanks again.
Jump to solution
6 Replies
mango1026
mango1026OP17mo ago
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?
Patrick Boivin
Patrick Boivin17mo ago
I'm curious, can people just change stock counts in the system, without any other kind of verification?
mango1026
mango1026OP17mo ago
Hi thanks for replying. Whoever that able to login into the system, they are automatically able to edit the stock counts.
Solution
mango1026
mango102617mo ago
But i have resolved this by using updateStateUsing. Thanks again.
jackoripo
jackoripo15mo ago
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. 👍
jackoripo
jackoripo15mo ago
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; }),
Want results from more Discord servers?
Add your server