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:
protected $appends = ['my_new_field'];

public function getMyNewFieldAttribute()
{
return $this->name . ' ' . $this->doc_num;
}
protected $appends = ['my_new_field'];

public function getMyNewFieldAttribute()
{
return $this->name . ' ' . $this->doc_num;
}
And then I want to use it in my Select field like this:
Select::make('decedent_id')
->preload()
->relationship('decedent', 'my_new_field')
->label(__('decedent_id_title'))
->options(Person::where('enabled',true)->where('type','causante')->pluck('my_new_field', 'id'))
Select::make('decedent_id')
->preload()
->relationship('decedent', 'my_new_field')
->label(__('decedent_id_title'))
->options(Person::where('enabled',true)->where('type','causante')->pluck('my_new_field', 'id'))
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
toeknee
toeknee16mo ago
Can you pluck from an append? Why not just format the option lavels?
Albert Lens
Albert LensOP16mo ago
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
$table->string('decedent_dropdown_description')->virtualAs('concat(full_name, " - ", doc_num, " ", COALESCE(DATE_FORMAT(date_of_death,"%d-%b-%Y", "es_es"), ""))');
$table->string('decedent_dropdown_description')->virtualAs('concat(full_name, " - ", doc_num, " ", COALESCE(DATE_FORMAT(date_of_death,"%d-%b-%Y", "es_es"), ""))');
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
toeknee
toeknee16mo ago
getOptionLabelsUsing
Albert Lens
Albert LensOP16mo ago
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.
No description
toeknee
toeknee16mo ago
Personaally I am not fmailiar with appended fields in filament, but the way I do that is:
->getOptionLabelFromRecordUsing(fn (MyModel $record) => $record->first_name.' - '.$record->last_name . ' - ' . $record->doc_num . ' - ' . $record->date_of_death)
->getOptionLabelFromRecordUsing(fn (MyModel $record) => $record->first_name.' - '.$record->last_name . ' - ' . $record->doc_num . ' - ' . $record->date_of_death)
Albert Lens
Albert LensOP16mo ago
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
toeknee
toeknee16mo ago
1. adjust your options query
php
->options(Person::where('enabled',true)->where('type','causante')->whereNotNull('date_of_death')->pluck('my_new_field', 'id'))
php
->options(Person::where('enabled',true)->where('type','causante')->whereNotNull('date_of_death')->pluck('my_new_field', 'id'))
Assuming you have null for date_of_death when empty 2. date('d-m-Y, strtotime($record->date_of_death))|
Albert Lens
Albert LensOP16mo ago
None of those works, sorry:
->options(Person::where('enabled',true)->where('type','causante')->pluck('full_name', 'id'))
->getOptionLabelFromRecordUsing(fn (Person $record) => $record->name.' - '. $record->doc_num . ' - ' . ($record->date_of_death) ? : date('d-m-Y', strtotime($record->date_of_death)))
->options(Person::where('enabled',true)->where('type','causante')->pluck('full_name', 'id'))
->getOptionLabelFromRecordUsing(fn (Person $record) => $record->name.' - '. $record->doc_num . ' - ' . ($record->date_of_death) ? : date('d-m-Y', strtotime($record->date_of_death)))
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.
No description
toeknee
toeknee16mo ago
Date format won’t work like that, you are checking if it has a date and returning nothing
Albert Lens
Albert LensOP16mo ago
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
->relationship('decedent', 'name')
->getOptionLabelFromRecordUsing(function (Person $record) {
return $record->name.' - ' . $record->surname01 . ' - ' . date('d-M-Y', strtotime($record->date_of_death));
})
->relationship('decedent', 'name')
->getOptionLabelFromRecordUsing(function (Person $record) {
return $record->name.' - ' . $record->surname01 . ' - ' . date('d-M-Y', strtotime($record->date_of_death));
})
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.
public function decedent(): BelongsTo
{
return $this->belongsTo(Person::class,'decedent_id')->where('type','causante');
}
public function decedent(): BelongsTo
{
return $this->belongsTo(Person::class,'decedent_id')->where('type','causante');
}
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.
Want results from more Discord servers?
Add your server