Laravel如何使用全部请求更新多个记录

时间:2019-03-04 04:52:28

标签: laravel

如何使用request->all()更新所有记录,我有很多列要更新。 这是我用于创建方法的代码,我正在使用该方法插入多个运行良好的新记录。

public function store(Request $request)
{
    $teacher = new Teacher;
    $teacher::create($request->all());
    $teacher->save();
    return back()->with('message','Teacher Added Successfully!');
}

这是我尝试过的方法,但是不起作用,

public function update(Request $request, Teacher $teacher)
{
    $teachers=$request->all();
    $teacher->save();
    return back()->with('message','Record Successfully Updated!');
}

2 个答案:

答案 0 :(得分:2)

代替此:

public function update(Request $request, Teacher $teacher)
{
    $teachers = $request->all();
    $teacher->save();
    return back()->with('message', 'Record Successfully Updated!');
}

您必须尝试

public function update(Request $request, Teacher $teacher)
{
    $input = $request->all();
    $teacher->fill($input)->save();
    return back()->with('message', 'Record Successfully Updated!');
}

假设您必须在fillable模型中创建Teacher属性。

我希望这会有所帮助。谢谢

答案 1 :(得分:0)

public function update(Request $request, Teacher $teacher)
{
    /*
     * For the love of god, validate your data!!!
     */
    $attributes = $this->validate($request, $rules);
    $teacher->update($attributes);

    return ...
}