0

I'm traying to fill a pivot table between two tables using Filament

equipos:

Schema::create('equipos', function (Blueprint $table) {
    $table->id();
    $table->string('nombre')->unique();           
    $table->timestamps();
});

jugadors

Schema::create('jugadors', function (Blueprint $table) {
    $table->id();
    $table->string('nombre');
    $table->string('apellidos');
    $table->string('email')->unique();
    $table->timestamps();
});

equipo_jugador:

    Schema::create('equipo_jugador', function (Blueprint $table) {               
        $table->foreignId('jugador_id')->references('id')->on('jugadors');
        $table->foreignId('equipo_id')->references('id')->on('equipos');
}

I have the relationships

class Equipo extends Model
{
use HasFactory;

    protected $fillable =['id','nombre', 'nombre_jugador_1' ,'nombre_jugador_2'];

    public function jugador1()
    {
        return $this->belongsToMany(Jugador::class);
    }

    public function jugador2()
    {
        return $this->belongsToMany(Jugador::class);
    }
}

class Jugador extends Model
{
    use HasFactory;

    protected $fillable=['id','nombre','apellidos','email'];

    public function equipos()
    {
        return $this->belongsToMany(Equipo::class);
    }
}

When i create a equipo i want to fill the pivot table

what is the best way to do this?

Thanks

Juan Diaz
  • 3
  • 1

1 Answers1

0

See filament docs #Populating automatically from a relationship

  1. Include with Pivot() in the EloquentModels (not sure why you have jugador1 and jugador2 functions and whether that works for Laravel. Shouldn't the function name match the model)

  2. Check the getRelations in the modelresource for both

Then it should be as simple as:

Forms\Components\Select::make('equipoId')
                ->multiple()
                ->relationship('equipos','nombre'),