angular - SQLSTATE [22007]:日期时间格式无效:1366 整数值不正确:Laravel 和 Angular 中“myapp”.“employees”.“state_id”列的“null”
问题描述
在一个项目中,我使用 Angular-12 作为前端,使用 Laravel-8 作为后端。
拉拉维尔:
$employee = Employee::find($id);
$employee->nationality_id = $request->nationality_id;
$employee->state_id = $request->state_id ?? '';
$employee->city_id = $request->city_id ?? '';
$employee->save();
角度:
零件:
loadCountryData(event: any) {
// this.profileInfoForm.patchValue({ state_id: null });
if (event) {
this.country_id = event.id;
this.cstatesService.getStatesByCountry(this.country_id).subscribe(
(data) => {
this.cstates = data.results.states;
this.state_id = '';
// console.log(this.cstates);
}
);
} else {
this.country_id = '';
}
}
loadStateData(event: any) {
// this.profileInfoForm.patchValue({ city_id: null });
if (event) {
this.state_id = event.id;
this.citiesService.fetchCitiesByState(this.state_id).subscribe(
(data) => {
this.cities = data.results.cities;
this.city_id = '';
}
);
} else {
this.state_id = '';
}
}
loadCityData(event: any) {
if (event) {
this.city_id = event.id;
} else {
this.city_id = '';
}
}
ngOnInit(): void {
this._id = this.route.snapshot.params['id'];
this.updateEmployee();
this.loadCountryData(event);
this.loadStateData(event);
this.loadCityData(event);
}
updateEmployee() {
this.profileInfoForm = this.fb.group({
nationality_id: ['', [Validators.required]],
state_id: [''],
city_id: [''],
});
}
onSubmitProfile() {
this.isSubmitted = true;
// stop here if form is invalid
if (this.profileInfoForm.invalid) {
return;
}
this.isLoading = true;
const formProfileData = this.profileInfoForm.getRawValue();
const formData = new FormData();
formData.append('nationality_id', formProfileData.nationality_id);
formData.append('state_id', formProfileData.state_id);
formData.append('city_id', formProfileData.city_id);
this.employeeService.updateProfile(this._id, formData).subscribe(res => {
this.data = res;
console.log(this.data);
// console.log(this.data);
this.tokenEditProfileHandler(res);
});
// this.isLoading = false;
}
<div class="row">
<div class="col-md-12">
<div class="row">
<div class="col-12 col-md-4">
<div class="form-group">
<label for="nationality">Nationality:<span style="color:red;">*</span></label>
<ng-select (change)="loadCountryData($event)" [items]="countries" [selectOnTab]="true" [searchable]="true" bindValue="id" bindLabel="nationality" placeholder="Select Nationality" [multiple]="false" [clearable]="true" required formControlName="nationality_id">
</ng-select>
</div>
<div *ngIf="fp.nationality_id.touched && fp.nationality_id.invalid">
<div *ngIf="fp.nationality_id.hasError('required')">
<div class="text-danger">
Nationality is required!
</div>
</div>
</div>
</div>
<div class="col-12 col-md-4">
<div class="form-group">
<label for="state_id">State of Origin:</label>
<ng-select (change)="loadStateData($event)" [items]="cstates" [selectOnTab]="true" [searchable]="true" bindValue="id" bindLabel="name" placeholder="Select State of Origin" [multiple]="false" [clearable]="true" formControlName="state_id">
</ng-select>
</div>
</div>
<div class="col-12 col-md-4">
<div class="form-group">
<label for="city_id">Town:</label>
<ng-select (change)="loadCityData($event)" [items]="cities" [selectOnTab]="true" [searchable]="true" bindValue="id" bindLabel="name" placeholder="Select Town" [multiple]="false" [clearable]="true" formControlName="city_id">
</ng-select>
</div>
</div>
</div>
</div>
</div>
这是一个依赖下拉列表。每当未选择 state_id 时,我都会收到此错误:
SQLSTATE [22007]:无效的日期时间格式:1366 不正确的整数值:列的“空”
myapp
。employees
.state_id
state_id, city_id 是整数。但是,如果其中有值,则插入它时不会出现任何错误。
我该如何解决这个问题?
谢谢
解决方案
就像@Panagiotis。如果模型保存或创建时不需要此字段,则员工表的迁移文件必须具有列修饰符“nullable()”
...
$table->unsignedBigInteger("state_id")->nullable();
...
之后,更改此语句
$employee->state_id = $request->state_id ?? '';
而是使用它
$employee->state_id = $request->state_id ?? null;
推荐阅读
- python - 正则表达式提取日期和特定字符串
- listview - Flutter/Dart 问题使用回调删除列表视图项
- android - Microsoft Bot 服务中的自定义数据错误
- angular - 是否可以在 Google Chrome 中添加指向子文件夹的快捷方式
- c# - 如何将 MediatR PublishStrategy 添加到现有项目
- angular - NGRX 8减速器返回对象而不是数组
- rest - 邮递员:每个请求发送不同的身体进行测试?
- android - Kotlin:如何用嵌套列表映射列表
- c# - 我有异常错误“System.Data.dll 中出现‘System.Data.SqlClient.SqlException’类型的未处理异常‘ID’附近的语法不正确。”
- c# - 如何获取 Azure Blob 存储中目录的“上次修改时间”属性