'Add data into a column of a pivot table [Laravel]
So basically I want to create an "appel" or a "registration" if you prefer.Into this you can add the name of the "creator" of the registration.All of that, is in my table "appel":
"id | createur | created_at | updated_at"
To continue I have my table student where all the student are with their informations:
"id | uuid | created_at | updated_at | first_name | last_name | email_address"
I've created a pivot table named "AppelStudent" to attach everything. Into this table I have the column "present" which is a boolean to know if the student was in the class or not. To finish I have the column "motif" to write a text if necessary.
"id | appel_id | student_id | present | motif"
I can create everything in my form expect the "motif", I don't find how to correctly attach the motif of the student.Like if I check the checkbox "present" of the student it won't save the motif.To be more understandable look at my code.
First this is my Appel controller
public function add_appel(Request $request){
$messages = [
'required' => 'Le champ ":attribute" est manquant',
'string' => ':attribute n\'est pas valide',
];
$attributes = [
'createur' => 'Createur de lappel',
// 'motif' => 'Motif élève', Problème avec motif
];
$validateData = Validator::make($request->all(), [
'createur' => 'required | string',
'present' => '',
], $messages, $attributes);
if ($validateData->fails()) {
$errors = $validateData->errors();
foreach ($errors->all() as $message) {
connectify('error','Erreur',$message);
}
return redirect("/appel/new");
}
else {
$appel = Appel::create([
"createur" => $request->createur,
]);
// $appel->minorstudents()->attach($request->minorstudents, [
// "present" => true,
// "motif" => $request->input("motif")[$index]
// ]
// );
foreach($request->minorstudents as $index => $minorstudent) {
$appel->minorstudents()->attach($request->minorstudents, [
"present" => true,
"motif" => $request->input("motif")[$index]
]);
}
// $appel->minorstudents()->attach(Minorstudent::whereNotIn("id", $request->minorstudents)->get());
foreach(Minorstudent::whereNotIn("id", $request->minorstudents)->get() as $index => $minorstudent) {
$appel->minorstudents()->attach($minorstudent, [
"motif" => $request->input("motif")[$index]
]);
}
$appel->save();
// $appel->minorstudents()->updateExistingPivot($request->minorstudents, ['motif' => $request->motif]);
// $appel->students()->attach($request->students, [
// "present" => true,
// // "motif" => $request->input('motif'),
// // "motif" => $request->motif,
// ]
// );
// $appel->students()->attach(Student::whereNotIn("id", $request->students)->get());
// $appel->students()->attach(["motif" =>$request->input('motif')]);
// $appel->students()->syncWithPivotValues([1],['motif' => $request->motif]);
// dd($appel->students()->motif = $request->motif);
// $appel->students()->attach($request->students);
// $appel->students()->updateExistingPivot($request->students, ['motif' => $request->input('motif')]);
return redirect("/appel");
}
}
This is my moigrations pivot table
public function up()
{
Schema::create('appel_student', function (Blueprint $table) {
$table->id();
$table->foreignId('appel_id')->constrained()->onDelete('cascade');
$table->foreignId('minorstudent_id')->constrained();
$table->boolean('present')->default(false);
$table->string('motif')->default('/');
$table->timestamps();
});
}
This is my form into my .blade
@foreach ($students as $student)
<tbody class="bg-white divide-y divide-gray-200">
<tr>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm font-medium text-gray-900">
{{$student->first_name}} //This show the informations of the student//
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm font-medium text-purple-900">
<input type="text" placeholder="Entrer un motif" name="motif[]" id="motif[]" value="/"> //This is the input of the "motif".You can write if you want a motif to the student//
</div>
</td>
<td class="px-6 py-4 whitespace-nowrap">
<div class="text-sm font-medium text-gray-900">
<input class="form-check-input" type="checkbox" name="students[]" type="checkbox" value="{{ $student->id }}" id="student-{{ $student->id }}" /> //This is my checkbox, everything work correctly.It correctly attach the boolean with the student into my pivot table
</div>
</td>
</tr>
</tbody>
@endforeach
Im sure it is something to deal with the controller.
It suppose to create something like this when I save the appel :
" All the things I need is to request the input "motif" and be saved like the image.My form is in the image too
Solution 1:[1]
In your Apple model
public function minorstudents()
{
return $this->belongsToMany(Student::class);
}
To set the motif for all the students you need to loop over each of the coming minorstudents values in the request BUT you must make the motif input name attribute such as motif[] because you give this input a motif and this is invalid
$appel = Appel::create(["createur" => $request->createur]);
foreach($request->minorstudents as $index => $minorstudent) {
$appel->minorstudents()->attach($request->minorstudents, [
"present" => true,
"motif" => $request->input("motif")[$index]
]);
}
foreach(Minorstudent::whereNotIn("id")->get() as $index => $minorstudent) {
$appel->minorstudents()->attach($minorstudent, [
"motif" => $request->input("motif")[$index]
]);
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
