F
Filament5mo ago
Ashk

Select relationship and "virtual" title attribute

Hello, I have a Customer model with a first_name and last_name field. A virtual field full_name which represents the concat of these 2 fields. In another resource, which has an belongsTo Customer relationship, I'd like a select based on full_name.
Forms\Components\Select::make('customer_id')
->searchable()
->relationship(
name: 'customer',
titleAttribute: 'email' # working well with email, but I would like to filter by full_name instead of email
),
Forms\Components\Select::make('customer_id')
->searchable()
->relationship(
name: 'customer',
titleAttribute: 'email' # working well with email, but I would like to filter by full_name instead of email
),
TY
5 Replies
Dennis Koch
Dennis Koch5mo ago
And what happens when you use full_name as the titleAttribute?
Ashk
AshkOP5mo ago
In Customer model
public function fullName(): Attribute
{
return new Attribute(
get: fn () => "{$this->first_name} {$this->last_name}"
);
}
public function fullName(): Attribute
{
return new Attribute(
get: fn () => "{$this->first_name} {$this->last_name}"
);
}
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'customers.full_name' in 'field list'
select
`customers`.`full_name`,
`customers`.`id`
from
`customers`
where
`customers`.`deleted_at` is null
order by
`customers`.`full_name` asc
select
`customers`.`full_name`,
`customers`.`id`
from
`customers`
where
`customers`.`deleted_at` is null
order by
`customers`.`full_name` asc
My engine is mariadb, even if I modify the query with something like ->addSelect(DB::raw("CONCAT(customers.first_name, ' ', customers.last_name) as full_name") , the engine doesn't allow to filter on this...
LeandroFerreira
LeandroFerreira5mo ago
you could create a virtual column
$table->string('full_name')->virtualAs('concat(first_name, \' \', last_name)');
$table->string('full_name')->virtualAs('concat(first_name, \' \', last_name)');
Ashk
AshkOP5mo ago
nice idea, it can solve my issue, but I'm curious if there is a way to solve that without a schema change thanks a lot, I mark as solved!
Dennis Koch
Dennis Koch5mo ago
I think the third param lets you modify the query and you could add the second where clause
Want results from more Discord servers?
Add your server