get maxvalue qty based on stock

hello, I want to set
maxValue
maxValue
on
TextInput::make('qty')->numeric()
TextInput::make('qty')->numeric()
from specific qty stock based on what the user chooses in
Select::make('inventory_id')
Select::make('inventory_id')
but as we know
Select
Select
isn't chosen yet by the user and make the
TextInput::make('qty')->numeric()
TextInput::make('qty')->numeric()
got an error
Forms\Components\Select::make('inventory_id')
->label('Inventory Name')
->distinct()
->required()
->live()
->options(Inventory::all()->pluck('asset_name', 'id'))
->searchable(),
Forms\Components\TextInput::make('qty')
->numeric()
->minValue(1)
->step(1)
->maxValue(function(Forms\Get $get, $record)
{
if (!empty($record))
return $record->inventory->qty-$record->inventory->inventory_location_list->count();
else return Inventory::where('id',$get('inventory_id'))->select(['qty'])->first()->qty;
})
->required(),
Forms\Components\Select::make('inventory_id')
->label('Inventory Name')
->distinct()
->required()
->live()
->options(Inventory::all()->pluck('asset_name', 'id'))
->searchable(),
Forms\Components\TextInput::make('qty')
->numeric()
->minValue(1)
->step(1)
->maxValue(function(Forms\Get $get, $record)
{
if (!empty($record))
return $record->inventory->qty-$record->inventory->inventory_location_list->count();
else return Inventory::where('id',$get('inventory_id'))->select(['qty'])->first()->qty;
})
->required(),
How could i tackle this problem? Thank you so much!
Solution:
oh I'm so sorry, i already found the answer ```->maxValue(function(Forms\Get $get, $record) { if (!empty($record)) ...
Jump to solution
5 Replies
Dennis Koch
Dennis Koch7mo ago
You could disable the field until the user chooses from the select.
thyk123
thyk123OP7mo ago
Hello! Thank you for your response, I appreciate it! I use this but still not working
->disabled(fn (Forms\Get $get): bool => !filled($get('inventory_id')))
->disabled(fn (Forms\Get $get): bool => !filled($get('inventory_id')))
Dennis Koch
Dennis Koch7mo ago
What is not working? What’s the errror you get?
thyk123
thyk123OP7mo ago
i got this
Attempt to read property "qty" on null
Attempt to read property "qty" on null
error shows when trying to create where inventory_id is not selected
Solution
thyk123
thyk1237mo ago
oh I'm so sorry, i already found the answer
->maxValue(function(Forms\Get $get, $record)
{
if (!empty($record))
return $record->inventory->qty-$record->inventory->inventory_location_list->count();
if (filled($get('inventory_id')))
return Inventory::where('id',$get('inventory_id'))->select(['qty'])->first()->qty;
else return '1';
})
->maxValue(function(Forms\Get $get, $record)
{
if (!empty($record))
return $record->inventory->qty-$record->inventory->inventory_location_list->count();
if (filled($get('inventory_id')))
return Inventory::where('id',$get('inventory_id'))->select(['qty'])->first()->qty;
else return '1';
})
Thank you so much for your help! @Dennis Koch
Want results from more Discord servers?
Add your server