在资源控制器的更新方法中,有可能使olny个表单字段具有相似的模型属性名称


Is possible get olny form fields have Similar Name of model properties in update method of resource controller

我有一个Course模型,它有很多像这样的字段:

course_id
title
description
creator
start_date
end_date
reg_start_date
reg_end_date
picture
lesson_count
cost
status
active
teacher
created_at
updated_at
deleted_at

我有一个表格来编辑指定的模型。编辑表单标签的动作属性被引用到CCD_ 2路由。

在编辑表单中,除了与上述Model属性同名的字段外,还有许多其他表单字段与Course Model无关(用于许多女性关系或其他操作)

现在在公共更新方法中,当我想使用Eloquent update()方法时,由于不相关字段名的数量很多,我必须对传入请求使用except()方法。像这样:

public
        function update (StoreCourseRequest $request, $id)
        {
            $data = $request->except(['search_node', '_token', 'start_date_picker', 'end_date_picker', 'reg_start_date_picker', 'reg_end_date_picker', 'orgLevels', 'courseCats','allLessonsTable_length']);
            $course = Course::findOrFail($id);
            $course->update($data);
            $course->org_levels()->sync($request->get('orgLevels'));
            $course->course_categories()->sync($request->get('courseCats'));
            $result = ['success' => true];
            return $result;
        }

正如您在$request->except()方法的使用中看到的那样,我向它传递了许多字段名,以便只过滤$course->update($data);中使用的适当属性。

现在我的问题是,有没有什么方法可以让我们从字段名称中只获得相同的名称模型属性?

如果我正确理解您的问题,您正在努力避免对传入请求使用except()方法,对吗?

如果是这种情况,您可以完全跳过它,并将整个请求传递给update()方法,因为它只会更新匹配的字段(前提是它们在方法类中列为"可填充")。这个过程被称为"批量分配"。