我有两个单独的表students
和tutors
。我也有数据透视表students_tutors
设置了两个值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获得批准,您可能需要标记该问题,因为它似乎是您所指的评论无处可寻。 –