F
Filament16mo ago
Askancy

Multiple select displays only one item

I have a select that allows multiple elements to be selected (they are currently in Edit). My select has selected elements id: 29,5 Which in the Platform table corresponds to: Xbox Series X, PC. He in the panel instead, shows me only Xbox Series X, fetching only one element.
Select::make('id_console[]')
->label('Platform:')
->relationship('platform','name')
->extraInputAttributes(['multiple' => true])
->searchable()
->required(),
Select::make('id_console[]')
->label('Platform:')
->relationship('platform','name')
->extraInputAttributes(['multiple' => true])
->searchable()
->required(),
public function platform()
{
return $this->belongsTo('App\Models\Platform', 'id_console', 'id');
}
public function platform()
{
return $this->belongsTo('App\Models\Platform', 'id_console', 'id');
}
4 Replies
cheesegrits
cheesegrits16mo ago
I'm a little confused. Your 'platform' relationship is a BelongsTo, so by definition can only have a single selection. And not sure what that extraInputAttributes is for? If you want to select mutiple options, you need to use ->multiple(), and the relationship itself has to be a BelongsToMany (or some other "many" relationship).
Askancy
AskancyOP16mo ago
I too am confused @Hugh Messenger 😂, with:
public function platforms()
{
return $this->hasMany('App\Models\Piattaforme', 'id', 'id_console');
}
public function platforms()
{
return $this->hasMany('App\Models\Piattaforme', 'id', 'id_console');
}
Select::make('id_console[]')
->label('Platform:')
->relationship('platforms','name')
->multiple()
->searchable()
->required(),
Select::make('id_console[]')
->label('Platform:')
->relationship('platforms','name')
->multiple()
->searchable()
->required(),
I get the error: >Filament\Forms\Components\Select::getRelationship(): Return value must be of type Illuminate\Database\Eloquent\Relations\BelongsTo|Illuminate\Database\Eloquent\Relations\BelongsToMany|Znck\Eloquent\Relations\BelongsToThrough|null, Illuminate\Database\Eloquent\Relations\HasMany returned
Brian Kidd
Brian Kidd16mo ago
He pointed out the relationship needs to be a BelongsToMany, not HasMany
cheesegrits
cheesegrits16mo ago
It wouldn't be a HasMany. It seems like what you need is a pivot table between whatever the model you are building this form on is, and the Platform model, so you can use a HasMany. https://laravel.com/docs/10.x/eloquent-relationships#many-to-many
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Want results from more Discord servers?
Add your server