将数据添加到数据透视表的列[Laravel]中



所以基本上我想创建一个;appel";或一个";注册";如果你愿意的话。您可以在其中添加";创建者";注册。所有这些,都在我的桌子上;appel":

"id | createur | created_at | updated_at">

为了继续,我有我的桌上学生,所有的学生都在那里,他们的信息:

"id | uuid | created_at | updated_at | first_name | last_name | email_address">

我创建了一个名为";AppelStudent";连接所有东西。在这张表中,我有一列";现在";这是一个布尔值,用来判断学生是否在课堂上。最后,我有一个专栏";主题";必要时写一篇课文。

"id|appel_id|student_id|present|motif">

我可以用我的形式创造一切;"基序";,我不知道如何正确地附加学生的主题。就像如果我选中复选框";现在";对于学生来说,这并不能拯救主题。为了更容易理解,请查看我的代码。

首先这是我的Appel控制器

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();
});
}

这是我的表格到我的.刀片

@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 

我确信这是控制器的问题。

当我保存appel:时,它假设会创建这样的东西

"我需要的所有事情都是请求输入";主题";并且像图像一样被保存。我的表格也在图像中

https://i.stack.imgur.com/WXh6b.png

https://i.stack.imgur.com/z5X7F.png

Apple型号中

public function minorstudents()
{
return $this->belongsToMany(Student::class);
}

要为所有学生设置motif,您需要在请求中的每个即将到来的minorstudents值上循环,但您必须使motif输入名称属性(如motif[](,因为您为该输入提供了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]
]);
}