所以基本上我想创建一个“开胃菜”或“注册”--如果你prefer.Into --你可以添加那个registration.All的“创建者”的名字,是在我的表“registration.All”中:
"id \ createur \ created_at \ updated_at“
为了继续,我有我的桌子学生,在那里所有的学生都有他们的信息:
"id \ uuid \ created_at,updated_at,updated_at,first_name,last_name,last_name,email_address“
我创建了一个名为"AppelStudent“的枢轴表来附加所有内容。在这个表中,我有一个列"present“,它是一个布尔值,用来判断学生是否在课堂上。为了完成我的专栏"motif“,如果必要的话,我可以写一篇文章。
"id \ appel_id \ student_id \“
我可以以我的形式创建一切--期待" motif ",如果我选中学生的“礼物”复选框,我找不到如何正确地附加student.Like的母题--它不能保存motif.To --看我的代码就更容易理解了。
首先,这是我的阿佩尔控制器
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");
}
}这是我的枢轴桌
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();
});
}这是我的.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 我肯定这是要处理控制器的事情。
当我保存开胃菜时,它应该创建这样的东西:
“我所需要做的就是请求输入"motif”,并像图像中的image.My表单一样保存
发布于 2022-03-24 12:41:07
在Apple模型中
public function minorstudents()
{
return $this->belongsToMany(Student::class);
}要为所有学生设置motif,您需要循环请求中的每个minorstudents值,但是您必须使输入名称属性(如),因为您为该输入提供了一个motif,这是无效的。
$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]
]);
}https://stackoverflow.com/questions/71601289
复制相似问题