F
Filament12mo ago
MrHex

Many to many relations with extra pivot field using Repeaters

Hi, I have a User model and a Course model there is also a course_user table with user_id, course_id and type fields. Relation from the Course model is:
public function users()
{
return $this->belongsToMany(User::class, 'course_user')
->withPivot('type')
->withTimestamps();
}
public function users()
{
return $this->belongsToMany(User::class, 'course_user')
->withPivot('type')
->withTimestamps();
}
Now i want to use this relation in a Repeater but the fields does not fill correctly.
1 Reply
karim charfaoui
karim charfaoui12mo ago
> public function usersCours(): HasMany
> {
> return $this->hasMany(CourseUser::class, 'course_user')
>
> }
> public function usersCours(): HasMany
> {
> return $this->hasMany(CourseUser::class, 'course_user')
>
> }
dont forget to create a CourseUser model as a pivot :
class CourseUser extend Pivot {
> protected $table ="course_user";
>
> public $incrementing = true; // if you have the id
>
> public function user(): BelongsTo
> {
> return $this->belongsTo(User::class);
> }
> public function course(): BelongsTo
> {
> return $this->belongsTo(Course::class);
> }
> }
class CourseUser extend Pivot {
> protected $table ="course_user";
>
> public $incrementing = true; // if you have the id
>
> public function user(): BelongsTo
> {
> return $this->belongsTo(User::class);
> }
> public function course(): BelongsTo
> {
> return $this->belongsTo(Course::class);
> }
> }
on your Resource :
> Forms\Components\Repeater::make('usersCours') // it must have the same name as your relationship
> ->label('')
> ->relationship()
> ->schema([
> ------you form
> ])
> Forms\Components\Repeater::make('usersCours') // it must have the same name as your relationship
> ->label('')
> ->relationship()
> ->schema([
> ------you form
> ])
Want results from more Discord servers?
Add your server