我有一个带有Laravel API的Angular应用程序,当我尝试更新它时,它会在这里生成验证错误
我有与Laravel API连接的Angular应用程序。在Laravel API中,员工和工资模型之间存在一对多的关系。
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');
}
型
更新控制器是
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::put('updateEmployee/{id}','App\Http\Controllers\EmployeeController@updateEmployee');
型
我的data.service.ts是这样的
updateData(id: string, data: any) {
return this.httpClient.put('http://127.0.0.1:8000/api/updateEmployee/'+id, data);
}
型
和site-edit.component.ts
updateEmployee() {
this.dataService.updateData(this.id, this.employee).subscribe(res => {
this.data = res;
this.employee = this.data;
console.log(res);
})
}
型
这是我最新的工资表
<h4>Salary Details</h4>
<div class="salary-details" *ngFor="let salary of employee.salaries">
<div class="table">
<div class="form-group row">
<label for="salary" class="col-sm-2 col-form-label">Salary</label>
<div class="col-sm-10">
<input type="text" name="salary" class="form-control" [(ngModel)]="salary.salary">
</div>
</div>
<br>
<div class="form-group row">
<label for="from_date" class="col-sm-2 col-form-label">From Date</label>
<div class="col-sm-10">
<input type="date" name="from_date" class="form-control" [(ngModel)]="salary.from_date">
</div>
</div>
<br>
<div class="form-group row">
<label for="to_date" class="col-sm-2 col-form-label">To Date</label>
<div class="col-sm-10">
<input type="date" name="to_date" class="form-control" [(ngModel)]="salary.to_date">
</div>
</div>
<br>
</div>
</div>
型
但当我尝试更新员工模型数据时,更新良好,但未更新薪金模型数据,检查响应确认,错误如下"The salary field is required. (and 2 more errors)"
"The salary field is required."
"The from date field is required."
The to date field is required.
那我怎么解决这个问题呢?
update我的dd($request->all());是
array:10 [ // app\Http\Controllers\EmployeeController.php:40
"emp_no" => 1
"birth_date" => "1983-10-05"
"first_name" => "bambu"
"last_name" => "Kannangara"
"gender" => "male"
"hire_date" => "2023-09-18"
"created_at" => null
"updated_at" => null
"salaries" => array:1 [
0 => array:6 [
"emp_no" => 1
"salary" => "9000"
"from_date" => "2023-09-22"
"to_date" => "2023-09-19"
"created_at" => "2023-10-09T08:17:26.000000Z"
"updated_at" => "2023-11-17T06:10:14.000000Z"
]
]
]
型
1条答案
按热度按时间mzmfm0qo1#
我不知道如何使用Angular,但根据
$request->all()
的结果,出现验证错误的原因是因为salary
,from_date
和to_date
无法根据您的验证规则检测到,因为它位于salaries
的数组中。要验证它们,您需要将验证规则更改为:
字符串