is it possible to group select items from query database
is it possible to group select from this
but get the group list from query database like this with category hasmany sub category:
thank you!
Solution:Jump to solution
Forms\Components\Select::make('payment_type_list_id')
->searchable()
->options(function () {
return PaymentTypeList::with('paymentType')
->get()...
17 Replies
Yes. You can use whereHas to get the data . And use groupby and mapwithkey to group the data based on what category.
Hi thank you so much for your help! But can I get an example, it's should not be exactly what I ask but could you give me a rough sketch of how to build in query builder laravel?
Many thanks!
Sure
Probably can you elaborate your model relationship here ? And what relationship you using for relation manager
Probably you can get some idea from my code
I have model document which contains belongsto relationship with documentType. And belongsto many with productCategories because i want to display documenttype based on what user set their productcategory. So it will show document depending on what user assosiate them with productCategory ( you can ignore this part)
In document type have attribute name and category ( ( i set label as status for category) which refer to what category i set for them ) . So in category i set either Wajib or Tambahan.
Down there is my code . Hopefully you can get some ideas
@FK21 thank you for you help!
i have 2 tables is PaymentTypes and PaymentTypeLists
each PaymentTypes has multiple PaymentTypeLists, so PaymentType hasMany PaymentTypeLists
so i want to make PaymentTypes as groups key and PaymentTypeLists as the options
where did you park your select field? in what resource ?
in InvoiceList resource, its inside repeater
what relationship between invoice and paymentType ? hasmany ?
so invoice hasmany paymentType
paymentType hasmany paymentTypeList
am i correct ?
correct!
Solution
Forms\Components\Select::make('payment_type_list_id')
->searchable()
->options(function () {
return PaymentTypeList::with('paymentType')
->get()
->groupBy('paymentType.name')
->mapWithKeys(function ($group, $key) {
return [$key => $group->pluck('name', 'id')];
});
});
probably you can try like this . havent test yet.
works like a charm!, Thank you so much for your help! I really appreciate for your help!
Great! Happy to help
Btw, do you know how to make the groups searchable? I think i need user can search the group title too.
So in this context it's become searchable for the paymentType.name
probably you can try like this ?
->getSearchResultsUsing(fn (string $search): array => {
return PaymentTypeList::query()
->whereHas('paymentType', function ($query) use ($search) {
$query->where('name', 'like', "%{$search}%");
})
->with('paymentType')
->get()
->mapWithKeys(function ($item) {
return [$item->id => $item->paymentType->name];
})
->toArray();
});
let me know if its work
thank you for your help!
i already test it but sadly it doesnt work
What output do you want from the search ?