skip to Main Content

I have 3 Models in My Laravel App like Employee , Salary and Title. Employee Model one to Many Relationship with both Salary and Title Models. Now I need update data using EmployeeController updateEmployee function using this function I need update some time all 3 tables tada also.
EmployeeController

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);

}

and my api route is following

Route::put('updateEmployee/{id}','AppHttpControllersEmployeeController@updateEmployee');

Employee Model

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

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

Salary Model

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

Title Model

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

but when I try update I got following error message
IlluminateDatabaseQueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'first_name' in 'field list' (SQL: update titlessetfirst_name= kevin,salary= 90000 wheretitles.emp_no= 10 andtitles.emp_nois not null) in file F:2023code2023apivendorlaravelframeworksrcIlluminateDatabaseConnection.php on line 760

how could I fix this?

2

Answers


  1. 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);
    }
    
    Login or Signup to reply.
  2. The best way u should do is parse your fields and thats it. So in controller you can have smth like this:

    public function store(PoseCreateRequest $request)
    {
        Pose::create($request->all());
    
        return redirect()->route('pose.index');
    }
    

    and in folder app/Http/Requests create PoseCreateRequest.php i mean create your own name and add

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

    and done, now back to controller and as usual in your $request->all() you have all fields, but when u make validation so your $request->all() contains only validate existing fields in request file. even if your request before has 10 fields, after this code u will return only your_fieldname_1 ,your_fieldname_1, captcha

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search