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

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

        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 using postman I got following error message
TypeError: IlluminateDatabaseEloquentBuilder::update(): Argument #1 ($values) must be of type array, null given, called in F:2023code2023apivendorlaravelframeworksrcIlluminateSupportTraitsForwardsCalls.php on line 23 in file F:2023code2023apivendorlaravelframeworksrcIlluminateDatabaseEloquentBuilder.php on line 1009

**
I really need update this 3 data tables when user need some update of these data tables. some time may be employees, some time employees and titles or some time all three tables with user need column or columns
**
how could I fix this problem?

2

Answers


  1. In the code you provided, the "expect value array null given" error could potentially occur in the following lines:

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

    The reason for this is that the update method is typically used to update records in the database, and it doesn’t return an array, second thing you are passing the value only to the function it expects array (key and value). Additionally, it returns a boolean value to indicate whether the update was successful or not.

    $employee->titles()->update(['title' => $request->title]);
    $employee->salaries()->update(['salary' => $request->salary]);
    

    Also please add an extra check on the employee query result.

    $employee = Employee::findOrFail($id);
    //OR
    $employee = Employee::find($id);
    if (!$employee) {
        return response()->json(['message' => 'Employee not found'], 404);
    }
    
    Login or Signup to reply.
  2. To resolve this issue, you should ensure that the $values variable is assigned an array before trying to use it. Here are some steps you can take to fix this problem:

    $employee = Employee::findOrFail($id);
    

    Before

    $title = $employee->titles()->update($request->title);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search