Problem to attach on relationManager
Hello,
I have a problem attaching one model to another with the relationmanager.
This is my model :
class Competition extends Model
{
public function squads()
{
return $this->belongsToMany(Squad::class)->join('clubs', 'club_id', '=', 'clubs.id')->orderBy('clubs.name');
}
}
class Squad extends Model
{
public function club()
{
return $this->belongsTo(Club::class);
}
}
I have a RelationManager :
class SquadsRelationManager extends RelationManager
{
protected static string $relationship = 'squads';
protected static ?string $recordTitleAttribute = 'clubs.name';
public function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('club.name'),
])
}
}
The club names are displayed correctly on the competition edit page, but when I want to associate a new club, I do a search in the club name list, and I get an error "Undefined property: stdClass::$name".
I don't know which "name" property is causing the problem.
Is it possible to add it like I did? I'm just starting out with Filament.
Thanks for your help.
(sorry for my english)Solution:Jump to solution
Thank you, it's OK with this code :
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['clubs.name'])
->recordTitle(fn (Squad $record): string => "{$record->club->name} ({$record->id})"),
...16 Replies
I'm not sure but I get a feeling
$recordTitleAttribute
is causing the issue...Thank you for your answer. In fact, by removing recordTitleAttribute, I no longer get an error and it works, but in the drop-down list for associating a club, I still get "squad" displayed.
How can I customize the display of this list to show the club name?
Can you share a quick screenshot?
I search for "borde" and all the data is displayed. the search is not done.
and I would like to search in the relationship ("clubs" table) and not "squads".
Just to make sure I understand: this is a Competition resource, and you're attaching some Squad in a relation manager, but you need to show the Club name in the select. Right?
yes
in my table squad, there is a belongsTo relation to Club
Ok... I think you will need to modify the select a little bit, have a look here:
And here:
i have already Try, but i have an error : Method Filament\Tables\Actions\AttachAction::modifyRecordSelectUsing does not exist.
Can you click the SHARE button at the top of the error page? Share the URL with me.
Oh ok, I think it's
recordSelect()
instead of modifyRecordSelectUsing()
Maybe the documentation needs to be updatedSolution
Thank you, it's OK with this code :
Tables\Actions\AttachAction::make()
->recordSelectSearchColumns(['clubs.name'])
->recordTitle(fn (Squad $record): string => "{$record->club->name} ({$record->id})"),
thank you, this saved my life