F
Filamentβ€’16mo ago
cpereiraweb

Accessor as a column in the Attach action on a RelationManager

Guys, I need help with a question: I have a RelationManager and I would like the Attach action form to display a combination of id and name of the associated model. I created an Accessor in the model, inserted it in the $appends array and used this Accessor as a column in the creation of the RelationManager. I did not succeed. Can anyone point me to a documentation link or other resource that points me in the right direction? Thank you all in advance for your help.
6 Replies
Patrick Boivin
Patrick Boivinβ€’16mo ago
Can you share your form code, in the attach action?
cpereiraweb
cpereirawebOPβ€’16mo ago
Sure:
<?php

namespace App\Models
//...
class Item extends Model
{
protected $appends = [
'item_name'
];

protected function itemName(): Attribute
{
return Attribute::make(
get: fn() => sprintf("%s - %s", $this->km_id, $this->description),
);
}
//...
<?php

namespace App\Models
//...
class Item extends Model
{
protected $appends = [
'item_name'
];

protected function itemName(): Attribute
{
return Attribute::make(
get: fn() => sprintf("%s - %s", $this->km_id, $this->description),
);
}
//...
->headerActions([
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['km_id', 'description'])
->preloadRecordSelect(),
])
->headerActions([
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['km_id', 'description'])
->preloadRecordSelect(),
])
cpereiraweb
cpereirawebOPβ€’16mo ago
Patrick, first of all, thank you for your attention. I saw this option but I understood that it would only be to customize the placeholder of the select. I'll take a look at the code for the class and see if I can find a way to do what I need. πŸ˜ƒ
LeandroFerreira
LeandroFerreiraβ€’16mo ago
try this
Tables\Actions\AttachAction::make()
->recordSelect(
fn () => Select::make('recordId')
->options(YourModel::pluck(DB::raw('concat(km_id, " - ", description)'), 'id'))
)
Tables\Actions\AttachAction::make()
->recordSelect(
fn () => Select::make('recordId')
->options(YourModel::pluck(DB::raw('concat(km_id, " - ", description)'), 'id'))
)
alpha
alphaβ€’11mo ago
Hi @Leandro Ferreira , any idea of doing this but keeping the select search functionality? This solution works but the select has all the options preloaded and don't do the search as the select that the AttachAction has.
Want results from more Discord servers?
Add your server