Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Flex's avatar
Level 4

Unable to Update both Title and Salary Tables same time

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 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}','App\Http\Controllers\EmployeeController@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');
    }

I can update Title or Salary Tables separately. but when I set both same time like

 $title = $employee->titles()->update($request->all());
  $salary = $employee->salaries()->update($request->all());

it is encounting following error Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'salary' in 'field list' (SQL: update titlessettitle= engineer,salary= 58000 wheretitles.emp_no= 2 andtitles.emp_no is not null) in file F:\2023\code\2023\api\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 760

Illuminate\Database\QueryException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'salary' in 'field list' (SQL: update titlessettitle= engineer,salary= 58000 wheretitles.emp_no= 2 andtitles.emp_nois not null) in file F:\2023\code\2023\api\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 760

0 likes
2 replies
DhPandya's avatar

@flex You're passing your request object directly to the update() function. Make sure your request object has the same keys which matches the column names in your titles table.

The titles tables does not have a columns for the salary so that's why it throws exception for unknown column.

Try to separate the data for each tables you want to update.

Snapey's avatar

Just send the data that is needed for each table.

but you are not going to get far with this approach. If an employee has multiple titles, how is Eloquent supposed to know WHICH title record you want to update?

Same for Salary

Why are these one to many relationships and not just employee columns?

Please or to participate in this conversation.