私は二つの別々のテーブルstudents
とtutors
を持っています。ピボットテーブルstudents_tutors
には、2つの値students_studentid
とtutors_tutid
が設定されています。物事の流れの中で、クライアントはチューターを学生に割り当てたり、その逆に、チューターを追加したり、後で編集したりすることができます。したがって、学生には多くの教師がおり、教師には多くの生徒がいるかもしれません。このような選択が行われると、ピボットテーブルの値が自動的に更新されるようになりました。複数の教師/生徒を一度に割り当てることができます。私はピボットテーブルの自動更新に問題があります。私はLaravelにとってかなり新しいです。誰もそれをどのように使うべきか、使用するコード、ピボット値を更新して表示する場所を誰にでも提案できますか?学生のためのピボットテーブルの値を自動的に更新する方法
マイadd.blade.php
:
<select name="tutid" id="tutors" multiple>
@foreach($tutors as $tutor)
<option value="{{$tutor->tutid}}">{{$tutor->name}}</option>
@endforeach
</select>
マイStudent
モデル:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $primaryKey = 'studentid';
public function user()
{
return $this->belongsTo(User::class);
}
public function Tutors()
{
return $this->belongsToMany(Tutors::class);
}
public function Invoices()
{
return $this->belongsToMany(Invoices::class);
}
}
マイTutor
モデル:
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Tutor extends Model
{
protected $guarded = [];
protected $primaryKey = 'tutid';
public function user()
{
return $this->belongsTo(User::class);
}
public function Students()
{
return $this->belongsToMany(Students::class);
}
}
私の学生コントローラーadd()
方法:
public function add(Student $student, Tutor $tutor)
{
$students = Student::all();
$tutors = Tutor::all();
return view('students.add', compact('students', 'tutors'));
}
あなたが何ができるかをここで
これまでに行ったことを示すことはできますか?私はモデルとマイグレーションを意味します。 – louisfischer
はい、お願いします。若干の研究努力を示してください:) – quinz
@louisfischerあなたが参照しているコメントのようであるため、https://stackoverflow.com/review/suggested-edits/17178443を承認したい場合は、質問にフラグを立てる必要があります。見つけることができません。 –