is it possible to group select items from query database

is it possible to group select from this
Select::make('status')
->searchable()
->options([
'In Process' => [
'draft' => 'Draft',
'reviewing' => 'Reviewing',
],
'Reviewed' => [
'published' => 'Published',
'rejected' => 'Rejected',
],
])
Select::make('status')
->searchable()
->options([
'In Process' => [
'draft' => 'Draft',
'reviewing' => 'Reviewing',
],
'Reviewed' => [
'published' => 'Published',
'rejected' => 'Rejected',
],
])
but get the group list from query database like this with category hasmany sub category:
Forms\Components\Select::make('payment_type_list_id')
->options([
category::query() => [
sub_category::query()]
])
Forms\Components\Select::make('payment_type_list_id')
->options([
category::query() => [
sub_category::query()]
])
thank you!
Solution:
Forms\Components\Select::make('payment_type_list_id') ->searchable() ->options(function () { return PaymentTypeList::with('paymentType') ->get()...
Jump to solution
17 Replies
Expecto Patronum
Yes. You can use whereHas to get the data . And use groupby and mapwithkey to group the data based on what category.
thyk123
thyk123OP7mo ago
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!
Expecto Patronum
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
thyk123
thyk123OP7mo ago
@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
class PaymentTypeList extends Model
{
use HasFactory;


public function payment_type(): BelongsTo
{
return $this->BelongsTo(PaymentType::class);
}
}

class PaymentType extends Model
{
use HasFactory,SoftDeletes;

public function payment_type_lists(): HasMany
{
return $this->HasMany(PaymentTypeList::class);
}
}
class PaymentTypeList extends Model
{
use HasFactory;


public function payment_type(): BelongsTo
{
return $this->BelongsTo(PaymentType::class);
}
}

class PaymentType extends Model
{
use HasFactory,SoftDeletes;

public function payment_type_lists(): HasMany
{
return $this->HasMany(PaymentTypeList::class);
}
}
Expecto Patronum
where did you park your select field? in what resource ?
thyk123
thyk123OP7mo ago
in InvoiceList resource, its inside repeater
FilamentTableRepeater\Forms\Components\TableRepeater::make('invoice_items')
->label('Items')
->relationship()
->schema([
Forms\Components\Select::make('payment_type_list_id')
->searchable()
->options(function (){
$lists=PaymentTypeList::first()->payment_type->id;
$paymentTypes = PaymentType::whereHas('payment_type_lists', function ($query) use ($lists)
{
$query->where('payment_type_list_id', $lists);
})
->get()
->groupBy('name');
dd($lists);
}),
])
FilamentTableRepeater\Forms\Components\TableRepeater::make('invoice_items')
->label('Items')
->relationship()
->schema([
Forms\Components\Select::make('payment_type_list_id')
->searchable()
->options(function (){
$lists=PaymentTypeList::first()->payment_type->id;
$paymentTypes = PaymentType::whereHas('payment_type_lists', function ($query) use ($lists)
{
$query->where('payment_type_list_id', $lists);
})
->get()
->groupBy('name');
dd($lists);
}),
])
Expecto Patronum
what relationship between invoice and paymentType ? hasmany ? so invoice hasmany paymentType paymentType hasmany paymentTypeList am i correct ?
thyk123
thyk123OP7mo ago
correct!
Solution
Expecto Patronum
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.
thyk123
thyk123OP7mo ago
works like a charm!, Thank you so much for your help! I really appreciate for your help!
Expecto Patronum
Great! Happy to help
thyk123
thyk123OP7mo ago
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
Expecto Patronum
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
thyk123
thyk123OP7mo ago
thank you for your help! i already test it but sadly it doesnt work
Expecto Patronum
What output do you want from the search ?
Want results from more Discord servers?
Add your server