Multiple Select with BelongsToMany relationship

I'm just recently using Filamentphp and need to get into the mindset of how it works.

I have two sql tables:

Article
Article_pivot_games

In article_pivot_games I have the columns:

  • id
    -article_id
    -games_id
now I need a multiple select, where he fills in the select with the names of the games that have the article_id equal to the id of the article I am editing.

Select::make('id_game[]')
    ->label('Games:')
    ->relationship('games', 'name')
    ->multiple()
    ->searchable()
    ->required(),  

in the model Article.php

public function games(): BelongsToMany
{
    return $this->belongsToMany(Games::class, 'article_pivot_games', 'games_id');
}


However, how do I tell the select to use that function? Also am I using the right method?

Before filamentphp I used a custom admin of my own, in which I had a column in Article with the id_games column containing "1,6,123,532," and I used this:

$pickup_games = explode(',', $articles->id_gioco);
$game_list = [];
foreach ($pickup_games as $pickup_game) {
    $game_list [] = Games::where('id', $pickup_game)->first();
}
Was this page helpful?