Storing repeater data in a pivot table

Hi ,ho to save data from repeater into pivot table? Here's the Models and the table I have. ---- Tables: expert :id, first_name,last_name,... diplome :id name,... diplome_experts : id,diplome_id,expert_id,year,... Expert Model :
public function diplome_expert(): BelongsToMany
{
return $this->belongsToMany(Diplome::class)
->withPivot('year', 'specialite_id', 'etablissement_id')
->withTimestamps();
}
public function diplome_expert(): BelongsToMany
{
return $this->belongsToMany(Diplome::class)
->withPivot('year', 'specialite_id', 'etablissement_id')
->withTimestamps();
}
Expert (Livewire)
Forms\Components\Section::make('Diplômes universitaires obtenus') ->schema([ Forms\Components\Repeater::make('diplome_expert')->label('Diplômes universitaires obtenus')
->relationship()
->schema([ Forms\Components\Select::make('diplome_id')->options(fn() => Diplome::pluck('name_fr', 'id')->toArray())->searchable()->label('Diplome'),
Forms\Components\TextInput::make('year')->label('Année'),

Forms\Components\Select::make('specialite_id')->options(fn() => Specialite::pluck('name_fr', 'id')->toArray())->searchable()->label('Spécialité'),
Forms\Components\Select::make('etablissement_id')->options(fn() => Etablissement::pluck('libelle_fr', 'id')->toArray())->searchable()->label('Etablissement'),

])->defaultItems(1)->createItemButtonLabel('Ajouter un autre dilpome universitaitre')->columns(2)->collapsible()->required(),
])->collapsed(),

]),
Forms\Components\Section::make('Diplômes universitaires obtenus') ->schema([ Forms\Components\Repeater::make('diplome_expert')->label('Diplômes universitaires obtenus')
->relationship()
->schema([ Forms\Components\Select::make('diplome_id')->options(fn() => Diplome::pluck('name_fr', 'id')->toArray())->searchable()->label('Diplome'),
Forms\Components\TextInput::make('year')->label('Année'),

Forms\Components\Select::make('specialite_id')->options(fn() => Specialite::pluck('name_fr', 'id')->toArray())->searchable()->label('Spécialité'),
Forms\Components\Select::make('etablissement_id')->options(fn() => Etablissement::pluck('libelle_fr', 'id')->toArray())->searchable()->label('Etablissement'),

])->defaultItems(1)->createItemButtonLabel('Ajouter un autre dilpome universitaitre')->columns(2)->collapsible()->required(),
])->collapsed(),

]),
Error captured : SQLSTATE[42S22]: Column not found: 1054 Unknown column 'diplome_id' in 'field list' (Connection: mysql, SQL: insert into **diplomes** (diplome_id, year, specialite_id, etablissement_id, updated_at, created_at) ------------ The data going to be stored in the 'diplome' table and not in the 'diplome_ experts' pivot table? Any help , Thanks
5 Replies
Dan Harrin
Dan Harrin15mo ago
you need to define a pivot model, and then a hasMany() relationship to that pivot model. you can use that hasmany as the repeater because then you are creating records in the pivot table, not the related table
ROOT-LEE
ROOT-LEE15mo ago
Thank you ! Your support means the world The pivot Model
class DiplomeExpert extends Pivot
{
use HasFactory;

protected $guarded = [];

}
class DiplomeExpert extends Pivot
{
use HasFactory;

protected $guarded = [];

}
The Expert Model with hasMany() relationship
public function diplome_expert(): HasMany
{
return $this->hasMany(DiplomeExpert::class);
}
public function diplome_expert(): HasMany
{
return $this->hasMany(DiplomeExpert::class);
}
Expert Ressource
Forms\Components\Repeater::make('diplome_expert')
->relationship()
->schema([/**/)]
Forms\Components\Repeater::make('diplome_expert')
->relationship()
->schema([/**/)]
God bless you
GavTheDev
GavTheDev15mo ago
This isn't working for me and I'm puzzled by why that is. I've seen a few other questions where the solution is the same, but whatever I do, nothing is showing up when I have saved the repeater data (or entered manually to test it). Also, saving doesn't automatically write the edit record id to the DB (as pictured in Amitav Roy's vid: https://www.youtube.com/watch?v=DKt7v_QSt18). I must be missing something, but what? 🧐
Amitav Roy
YouTube
14 Mastering Laravel Filament: Building a Repeater with Relationship
Are you looking to take your Laravel Filament skills to the next level? In this tutorial, we'll walk you through the process of building a repeater with a relationship using Laravel Filament. With the help of our expert instructors, you'll learn how to leverage the power of Laravel Filament to create powerful, dynamic interfaces for your web app...
Dan Harrin
Dan Harrin15mo ago
please send the code also are you using this outside the admin panel? its probably best if you add all the details in a new theead
GavTheDev
GavTheDev15mo ago
Hey! No, using it in admin panel @danharrin Sooo, I think I found the issue. My pivot table didn't have an ID on it. Adding that resolved it 🤦‍♂️
Want results from more Discord servers?
Add your server
More Posts