filament many to many
how to create resource with many to many relationships content additional field ??
9 Replies
What‘s a „many to many content additional field“?!
i have two models Student and Classroom link with many to many. i have a table with classroom_id, student_id and one additional field(academic_id). hope it's more readable
i whish to know how i can field academic_id who is addition field in these relation
I still don’t really understand what’s the issue. But have you looked at relation managers and the repeater?
Are you talking about pivot fields?
this is the error i get: insert into
classroom_student
(classroom_id
, student_id
) values (1, 1)
SQLSTATE[HY000]: General error: 1364 Field 'academic_id' doesn't have a default value
in this relation classroom_student, academic_id is an additional fieldSorry but I can’t help you if you if you just share an error without even knowing what you do.
The error says you are missing a required field.
function in my student model: public function classrooms() {
return $this->belongsToMany(Classroom::class)->withPivot('academic_id');
}
function in my classroom model: public function students() {
return $this->belongsToMany(Student::class,)->withPivot('academic_id');
}
student resource form public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Section::make('Information Personnel Apprenant')
->schema([
Forms\Components\TextInput::make('fname')
->label('Nom')
->autofocus()
->required()
->maxLength(255)
->autocapitalize(),
Forms\Components\TextInput::make('lname')
->label('Prenom')
->maxLength(255)
->autocapitalize(),
Forms\Components\Radio::make('sexe')
->options([
1 => 'Homme',
0 => 'Femme',
])
->boolean()
->inline()
->default(1)
->required(),
Forms\Components\DatePicker::make('born_at')
->label('Date de Naissance')
->required(),
Forms\Components\TextInput::make('born_place')
->label('Lieu de naissance')
->required()
->maxLength(255),
Forms\Components\TextInput::make('quarter')
->label('Quartier Habitation')
->maxLength(255)
->required()
])
->columns(2),
]);
}
and this class CreateStudent extends CreateRecord
{
protected static string $resource = StudentResource::class;
protected function mutateFormDataBeforeCreate(array $data): array
{
$data['matricule'] = '122';
$data['academic_id'] = 12;
return $data;
}
}
Please read #✅┊rules about code formatting.
my migrations
my models