Laravel更新了一对多的关系

时间:2017-05-26 22:23:23

标签: php mysql laravel eloquent one-to-many

我有两个表,第一个是主题表,第二个是 subjects_doctors表。这里的关系是一对多

  

更新方法代码:

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表中更新它们。

1 个答案:

答案 0 :(得分:2)

使用Laravel中的 sync()功能解决了该问题。感谢。

  

这是它可能提供帮助的代码

x = ["pink-one", "pink-two", "pink-three", "pink-four"]
y = ["pink", "one"]  # For a search to be successful, all of these
                     # strings should be present in `x`.
find_multiple_substring(x, y)   # returns 0