我有兩個單獨的表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獲得批准,您可能需要標記該問題,因爲它似乎是您所指的評論無處可尋。 –