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:
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:
For example, it shows "Blood group: 1" instead of "Blood group: A+". How can I fix this?
Solution:Jump to 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....
14 Replies
try:
blood_group.name
assuming you have a relationship of blood_group and the label A+ exists in column name
NO this is just an array that I was using.
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?
Can I use label function to get data from this array>
Can you provide the whole code, becasue it's not making sense to me what you are doing
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
Are the blood groups stored in a database table?
i.e. a list of blood groups
No they are not stored inside the table
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.
Creating a table and adding relationship looks better options here
Great! That'll work natively then π
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
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.
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