php 未找到列:Laravel 9中的1054未知列

1tuwyuhd  于 2023-09-29  发布在  PHP
关注(0)|答案(2)|浏览(164)

我在我的Laravel应用程序中有3个模型,如员工,工资和头衔。具有薪资和职称模型的员工模型一对多关系。现在我需要更新数据使用EmployeeController updateEmployee函数使用这个函数我需要更新一些时间所有3表tada也。员工控制器

public function updateEmployee(Request $request, $id) {
    $employee = Employee::find($id);
    $title = $employee->titles()->update($request->all);
    $salary = $employee->salaries()->update($request->all);
    if(is_null($employee)) {
        return response()->json(['message' => 'Employee not found'], 404);
    }
    $employee->update($request->all());
    
    return response($employee, 200);

}

我的API路由是

Route::put('updateEmployee/{id}','App\Http\Controllers\EmployeeController@updateEmployee');

员工模型

public function titles(): HasMany
{
    return $this->hasMany(Title::class, 'emp_no');
}

public function salaries(): HasMany
{
    return $this->hasMany(Salary::class, 'emp_no');
}

薪酬模式

public function employee(): BelongsTo
{
    return $this->belongsTo(Employee::class, 'emp_no');
}

标题模型

public function employee(): BelongsTo
{
    return $this->belongsTo(Employee::class, 'emp_no');
}

但当我尝试更新我得到以下错误消息Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'first_name' in 'field list' (SQL: update标题set名字= kevin,工资= 90000 where标题. emp_no = 10 and标题. emp_no is not null) in file F:\2023\code\2023\api\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 760
我该怎么弥补?

col17t5w

col17t5w1#

The issue is that you are trying to update all columns at once by passing $request->all() to the update method, which is causing Laravel to generate an incorrect SQL query.

You should update the related records separately and specify which columns you want to update. Here's how you can fix your updateEmployee function:

public function updateEmployee(Request $request, $id)
{
    $employee = Employee::find($id);

    if (is_null($employee)) {
        return response()->json(['message' => 'Employee not found'], 404);
    }

    // Update employee information
    $employee->update([
        'first_name' => $request->input('first_name'),
        'other_employee_fields' => $request->input('other_employee_fields'),
        // Add other employee fields as needed
    ]);

    // Update related titles
    $employee->titles->update([
        'title_field' => $request->input('title_field'),
        // Add other title fields as needed
    ]);

    // Update related salaries
    $employee->salaries->update([
        'salary_field' => $request->input('salary_field'),
        // Add other salary fields as needed
    ]);

    return response($employee, 200);
}
sy5wg1nm

sy5wg1nm2#

你应该做的最好的方法是解析你的字段,就是这样。所以在controller中,你可以像这样设置smth:

public function store(PoseCreateRequest $request)
{
    Pose::create($request->all());

    return redirect()->route('pose.index');
}

在文件夹app/Http/Requests创建PoseCreateRequest.php我的意思是创建自己的名字和添加

public function rules(): array
    {
        return [
            'your_fieldname_1' => ['required',],
            'your_fieldname_2' => ['required'],
            'captcha' => ['required', 'captcha'],
        ];
    }

完成,现在回到控制器,像往常一样,在你的$request->all()中你有所有的字段,但是当你进行验证时,你的$request->all()只包含验证请求文件中的现有字段。即使你之前的请求有10个字段,在这段代码之后,u只会返回your_fieldname_1your_fieldname_1captcha

相关问题