Prevent multiple calls to the same model

I have the following scenario

Select::make('inventory_id')
    ->relationship('inventory', 'title')
    ->searchable()
    ->preload()
    ->live()
    ->required(),
TextInput::make('quantity')
    ->required()
    ->numeric()
    ->maxValue(fn(Get $get) => $get('inventory_id') ? Inventory::find($get('inventory_id'))->quantity : null)
    ->helperText(fn (Get $get) => $get('inventory_id') ? ['quantity' => Inventory::find($get('inventory_id'))->quantity] : null),


Basically, I'm setting a maxValue on the quantity field. I'm also adding a helperText to give the user a more helpfull message. But I'm calling the same thing twice.

Any recommendations to avoid doing this?
Solution
I guess that's up to you to decide. I just said that because sometimes an extra find query is no big deal.

In any case, if you're in laravel 11+, have a look at this too https://flanger.dev/blog/post/avoid-duplicate-queries-in-filament-closures-for-eloquent-records
Was this page helpful?