getSearchResultsUsing() access other formdata

Hey, Is it possible to access order data from the resource besides the $search string? For Example I would like to get only the search result for the selected brand, Any suggestions?
11 Replies
JJSanders
JJSandersOP11mo ago
Bumping this: how to access the other data from the form in getSearchResultsUsing()?
awcodes
awcodes11mo ago
Is the brand a filter on the table or is the app scoped to the brand. Better question, are you trying to update a table from a form. Or are you trying to simply filter a table?
JJSanders
JJSandersOP11mo ago
The table is in the OrderResource in order to show order items. But you should only be able to select products from the supplier which has been selected when creating the order
awcodes
awcodes11mo ago
You can adjust the table query with $table->modifyQueryUsing(). But I’m not sure where you’re going to get the supplier from to start with. Maybe a select filter would be better than a search.
JJSanders
JJSandersOP11mo ago
I have over 20k products to choose from.... So select filter is not an option really
awcodes
awcodes11mo ago
Well, as long as supplier is a field on the column I would expect the search to filter it correctly. You could make the select searchable. And preload it with like 25 options. To help with the db hit. Unless you know the supplier upfront then modifying the table query up front isn’t going to do anything for you. It’s possible I’m misunderstanding you too.
JJSanders
JJSandersOP11mo ago
I added a screenshot to make it more visual. Now as you can see in the code below: 1. In the options I am able to filter the first chunk by the supplierId. 2. In the getSearchResultsUsing I can in fact search for anything because it doesn't filter by supplier_id
Select::make('product_id')
->label('Product')
->hidden(function () use ($productId) {
return $productId !== null;
})
->options(function (Get $get) {
$supplierId = $get('../../supplier_id');
if ($supplierId) {
return Product::select('id', DB::raw("CONCAT(item_code, ' - ', item_description) as concatenated_value"))
->where('supplier_id', $supplierId)
->where('available', true)
->limit(25)
->pluck('concatenated_value', 'id')
->toArray();
}

return ['Selecteer eerst een merk'];
})
->searchable()
->debounce(1200)
->getSearchResultsUsing(fn (string $search): array =>
Product::select('id', DB::raw("CONCAT(item_code, ' - ', item_description) as concatenated_value"))
->where('available', true)
->having('concatenated_value', 'like', "%{$search}%")
->limit(50)
->pluck('concatenated_value', 'id')
->toArray())
->getOptionLabelUsing(function ($value): ?string {
$product = Product::select(DB::raw("CONCAT(item_code, ' - ', item_description) as concatenated_value"))
->where('available', true)
->having('concatenated_value', 'like', "%{$value}%")
->first();

return $product ? $product->concatenated_value : null;
})

->preload()
->disabled(fn(Get $get) => $get('../../sent'))
->columnSpan(2)
->required(),
Select::make('product_id')
->label('Product')
->hidden(function () use ($productId) {
return $productId !== null;
})
->options(function (Get $get) {
$supplierId = $get('../../supplier_id');
if ($supplierId) {
return Product::select('id', DB::raw("CONCAT(item_code, ' - ', item_description) as concatenated_value"))
->where('supplier_id', $supplierId)
->where('available', true)
->limit(25)
->pluck('concatenated_value', 'id')
->toArray();
}

return ['Selecteer eerst een merk'];
})
->searchable()
->debounce(1200)
->getSearchResultsUsing(fn (string $search): array =>
Product::select('id', DB::raw("CONCAT(item_code, ' - ', item_description) as concatenated_value"))
->where('available', true)
->having('concatenated_value', 'like', "%{$search}%")
->limit(50)
->pluck('concatenated_value', 'id')
->toArray())
->getOptionLabelUsing(function ($value): ?string {
$product = Product::select(DB::raw("CONCAT(item_code, ' - ', item_description) as concatenated_value"))
->where('available', true)
->having('concatenated_value', 'like', "%{$value}%")
->first();

return $product ? $product->concatenated_value : null;
})

->preload()
->disabled(fn(Get $get) => $get('../../sent'))
->columnSpan(2)
->required(),
I hope this helps to clarify my question.
awcodes
awcodes11mo ago
You don’t need options() and getSearchResultsUsing() and to restrict it to a a brand you just need to add the brand as another where() clause.
JJSanders
JJSandersOP11mo ago
Ok. But how then, do i get the brand in the getSearchResultsUsing. In other words, how do I refer to the selected supplier in the where() clause?
awcodes
awcodes11mo ago
Depends on where you’re getting brand from. If it’s a separate filter you can get it from the query string. If it’s another field in this same filter form then you can use $get. You would just have two where methods. They can be chained.
JJSanders
JJSandersOP11mo ago
Ahhhhhh I didn't think $get would work in this closure. I always find it hard that when you are given the option to pass a closure, to see what can be passed as arguments.
Want results from more Discord servers?
Add your server