0
我有兩個表第一個是科目表第二個是subjects_doctors表。這裏的關係是一對多。Laravel更新一對多關係
更新方法的代碼:
public function update(Request $request, $id)
{
$this -> validate($request, [
'name' => 'required',
'max_students' => 'required',
'doctors' => 'required',
'description' => 'required'
]);
$subject = Subject::find($id);
$subject -> name = $request -> name;
$subject -> description = $request -> description;
$subject -> max_students = $request -> max_students;
$subject -> save();
//PROBLEM
$y = Subject::where('id', $subject -> id) -> doctors() -> update(['doctor_id' => $request -> doctors]);
Session::flash('success', 'Subject has been updated successfully!');
return redirect() -> route('subjects.index');
}
$request
的變量包含具有所有更新的醫生ID陣列。
對於實例
我在科目表是subject 1 HAVE 1 DOCTOR
而且我已經更新到SUBJECT 1 HAVE 2 DOCTORS
..我怎麼做呢?
我有一切變數,但我無法在subjects_doctors
表中更新它們。