validation errors? what's inside error: {…} object?
Unable to Update Laravel API One to Many Relationship data on Angular Frontend
I have Angular App connected with Laravel API. In Laravel API there are one to many relationship between Employee and Salary Models. Employee.php
protected $fillable = ['birth_date','first_name','last_name','gender','hire_date'];
protected $primaryKey = 'emp_no';
public function salaries(): HasMany
{
return $this->hasMany(Salary::class, 'emp_no','emp_no');
}
Salary.php
protected $fillable = ['salary','from_date','to_date'];
public function employee(): BelongsTo
{
return $this->belongsTo(Employee::class, 'emp_no');
}
EmployeeUpdate Controller is
public function updateEmployee(Request $request, $id)
{
$employee = Employee::find($id);
if (is_null($employee)) {
return response()->json(['message' => 'Employee not found'], 404);
}
$employee->update($request->validate([
'emp_no' => 'required',
'first_name' => 'required',
'last_name' => 'required',
'gender' => 'required',
'hire_date' => 'required'
]));
$employee->salaries()->update($request->validate([
'emp_no' => 'required',
'salary' => 'required',
'from_date' => 'required',
'to_date' => 'required'
]));
return response($employee, 200);
}
api route for update
Route::put('updateEmployee/{id}','App\Http\Controllers\EmployeeController@updateEmployee');
my data.service.ts is like this
updateData(id: string, data: any) {
return this.httpClient.put('http://127.0.0.1:8000/api/updateEmployee/'+id, data);
}
and employee-edit.component.ts
updateEmployee() {
this.dataService.updateData(this.id, this.employee).subscribe(res => {
this.data = res;
this.employee = this.data;
console.log(res);
})
}
but when I try to update Employee Model data is updating well but not updating Salary Model data inspect encounting folllowing error
ERROR Object { headers: {…}, status: 422, statusText: "Unprocessable Content", url: "http://127.0.0.1:8000/api/updateEmployee/1", ok: false, name: "HttpErrorResponse", message: "Http failure response for http://127.0.0.1:8000/api/updateEmployee/1: 422 Unprocessable Content", error: {…} }
how could I fix this problem?
Please or to participate in this conversation.