Errors in Select with new field Attribute appended to eloquent model
Hello.
I need a bunch of appended fields to several of my models. I have followed the normal Laravel usage for this:
I define the accessor and add Attribute in model:
And then I want to use it in my Select field like this:
And I get the error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'my_new_field' in 'field list'
I have checked using Tinker and it works fine.
Any idea of what can be happening?
Tks.
11 Replies
Can you pluck from an append?
Why not just format the option lavels?
I have been trying quite a lot of different things.
I followed documentation and found that for select and pluck methods I should use migrations and virtualAs. I did so, and it works, but as it includes dates they are shown formatted in English and I need them formatted in Spanish
I use the above code, but it works in local but throws an error in Forge.Laravel saying that MySQl does not admit the third parameter in DATE_FORMAT for the locale.
And I am lost going round and round. And I do need quite a bunch of those CONCAT fields
Farr too complicated, when you can just use a closure with:
https://filamentphp.com/docs/3.x/forms/fields/select#returning-custom-search-results
getOptionLabelsUsing
Sorry but I don't get it.
It is not only a problem of searching. The first thing I need is to show a concat of several fields, some of those formatted, in the dropdown.
Like the image shown. The problems is just the format of the date, and that is why I would need to use eloquent to format with carbon and so on.
Really sorry for my not understanding, but can someone answer these:
1.- It is NOT POSSIBLE to use pluck with an appended field in Filament?
2.- If not, what would be the approach for this Select() in filament forms?
Tks again.
Personaally I am not fmailiar with appended fields in filament, but the way I do that is:
Thanks a lot.
It really is very interesting, but for some cases.
But here I have 2 problems with this solution:
1.- How or where can I filter the records I want to show? I only want those with date_of_death
2.- How can I format the date_of_death - I have used date_format($record->date_of_death, 'd-m-Y') but nothing different happens. It shows dates in yyyy-mm-dd
1. adjust your options query
Assuming you have null for date_of_death when empty
2. date('d-m-Y, strtotime($record->date_of_death))|
None of those works, sorry:
When I use the ->options() it overwrites teh getOptionLabelFromRecordUsing; In this case it only shows full_name which is in the pluck.
Format for the date is not working, either.
Date format won’t work like that, you are checking if it has a date and returning nothing
Ah, Ok. Sorry.
Tks
Anyway I cannot use the where condition or filter only the Persons that I need.
I found a solution without the need of creating a virtualAs column in the migration (though it is a good solution also):
As we cannot filter the SELECT without using ->options() and then, getOptionLabelFromRecordUsing is overwritten by options, finaly I have:
1.- Used only relationship + getOptionLabelFromRecordUsing, but with no options
2.- Then, to prefilter only the people I need, which are those with type == 'causante', I have changed the relationship in the model Dossier adding the where condition there.
This way, in a simple way which allows to add the createOptionForm and editOptionForm easily I have only the list of people I need and the concat text I want.
Hope it can help anybody.