F
Filamentβ€’2y ago
Debjit

Custom Group Label

I am working on a Laravel Filament project and I have a problem with grouping a resource by blood group. I have an array that maps blood group ids to blood group names, like this:
$bloodGroups = [
1 => 'A+',
2 => 'B+',
3 => 'AB+',
4 => 'O+',
5 => 'A-',
6 => 'B-',
7 => 'AB-',
8 => 'O-'
];
$bloodGroups = [
1 => 'A+',
2 => 'B+',
3 => 'AB+',
4 => 'O+',
5 => 'A-',
6 => 'B-',
7 => 'AB-',
8 => 'O-'
];
I store the blood group id as an integer in the database, but I want to display the blood group name in the resource. However, when I use the groups method, it shows the id instead of the name, like this:
->groups([
'required_on',
'blood_group', // this shows the id, not the name
])
->groups([
'required_on',
'blood_group', // this shows the id, not the name
])
For example, it shows "Blood group: 1" instead of "Blood group: A+". How can I fix this?
Solution:
If you put them in a table, then you can use: https://filamentphp.com/docs/3.x/tables/grouping#setting-a-grouping-label Else, remove the interfers and just create string array of groups and ensure the users enter as a string and not a number....
Jump to solution
14 Replies
toeknee
toekneeβ€’2y ago
try: blood_group.name assuming you have a relationship of blood_group and the label A+ exists in column name
Debjit
DebjitOPβ€’2y ago
NO this is just an array that I was using.
toeknee
toekneeβ€’2y ago
Can you provide your whole code? IF you are grouping by groups and it's rendering the Key why not not use the name as key? For sorting?
Debjit
DebjitOPβ€’2y ago
Can I use label function to get data from this array>
toeknee
toekneeβ€’2y ago
Can you provide the whole code, becasue it's not making sense to me what you are doing
Debjit
DebjitOPβ€’2y ago
In database there are just integer / numeric value is storing. the valuse are inside an function sure Here is a form schema, Select::make('blood_group')->options([ 1 => 'A+', 2 => 'B+', 3 => 'AB+', 4 => 'O+', 5 => 'A-', 6 => 'B-', 7 => 'AB-', 8 => 'O-' ])->required(), inside database just number is storing. when I need to use name I am using $bloodGroups[$id] to display
toeknee
toekneeβ€’2y ago
Are the blood groups stored in a database table? i.e. a list of blood groups
Debjit
DebjitOPβ€’2y ago
No they are not stored inside the table
Solution
toeknee
toekneeβ€’2y ago
If you put them in a table, then you can use: https://filamentphp.com/docs/3.x/tables/grouping#setting-a-grouping-label Else, remove the interfers and just create string array of groups and ensure the users enter as a string and not a number.
Debjit
DebjitOPβ€’2y ago
Creating a table and adding relationship looks better options here
toeknee
toekneeβ€’2y ago
Great! That'll work natively then πŸ™‚
Debjit
DebjitOPβ€’2y ago
Thanks, But that would be grt if I just pass the array and then let it group over these param. But this also worked. Thank mate
toeknee
toekneeβ€’2y ago
Yeah that's a weird scenario tbh, usually you can pass an array. But you are using a keyed array and as such those keys should exist somewhere in a database. Otherwise you should just be using text.
Debjit
DebjitOPβ€’2y ago
Yes that is true. Its just sitting there for iternity and I do not think there will be another blood group in near future though πŸ˜‚ Thank mate for prompt support

Did you find this page helpful?