First I am using filament form builder not panel.
My question:
I have a ManyToMany relationship in filament, and when I save the form everything works very well, the main model and these relations saved perfectly, but I need to fill some value in pivot table because the child was selected in base these columns value in pivot table.
schema of relationship
When I create Famille entity with Personne as relationships, it will create perfectly but without Categorie id, how can I specify the pivot attribute?
Benzaza Laid is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You could use a Filament Repeater inside your form, but that means you also need to create a pivot model to manage your model relationships.
If you want to do it this way:
Step 1
Create the PersonneFamilie pivot model:
// app/Models/PersonneFamille.php
<?php
namespace AppModels;
use IlluminateDatabaseEloquentRelationsBelongsTo;
use IlluminateDatabaseEloquentRelationsPivot;
class PersonneFamille extends Pivot
{
protected $table = 'personne_famille';
public function famille(): BelongsTo
{
return $this->belongsTo(Famille::class);
}
public function personne(): BelongsTo
{
return $this->belongsTo(Personne::class);
}
public function categorie(): BelongsTo
{
return $this->belongsTo(Categorie::class);
}
}
Step 2
Update the involved models (replace the relation functions in each of the 3 models to be able to access the relationship from the models):
//rename the function it if needed
public function personneFamille()
{
return $this->hasMany(PersonneFamille::class);
}
Step 3
Add the repeater field in the form. If the form is inside Famille resource, add the two other fields from the pivot table:
//FamilleResource.php
//use FilamentFormsComponentsRepeater;
public static function form(Form $form): Form
{
return $form
->schema([
Repeater::make('personneFamille') //the function name from step 2
->relationship()
->schema([
Select::make('personne_id')
->relationship('personne', 'name') //replace 'name' with your desired column
->required(),
Select::make('categorie_id')
->relationship('categorie', 'name') //replace 'name' with your desired column
->required(),
])
->columns(2), //optional
// ...
Pivot table source from: Laravel documentation
Repeater field source from: Filament documentation
2