【发布时间】:2020-03-29 19:44:40
【问题描述】:
当我尝试以 Json 格式将数据发布到 api 时,我收到错误:
“违反完整性约束:1048 列 'school_id' 不能为空”
这是我的 js (vuejs)
if (this.editD === false) {
fetch('/api/school/'+this.location.school_id+'/location/store', {
method: 'post',
body: JSON.stringify(this.location),
header: {
'content-type': 'application/json'
}
})
.then(res => res.json())
.then(data => {
this.emptyLocation();
this.editD = false;
this.fetchSchools();
})
.catch(err => console.log(err));
}
我尝试返回正在发送的正文值,以验证它是否是有效的 Json 格式并且是:
{
"id": "",
"school_id": 1,
"department_name": "test",
"department_address": "etst",
"department_zip": "ttest",
"department_city": "set",
"department_phone": "32324"
}
我的 Api 路由如下所示:
Route::post('/school/{id}/location/store', 'Owner\ApiController@storeLocation');
控制器方法是这样的:
public function storeLocation(request $request) {
$department = new Department;
$department->school_id = $request->input('school_id');
$department->department_name = $request->input('department_name');
$department->department_address = $request->input('department_address');
$department->department_zip = $request->input('department_zip');
$department->department_city = $request->input('department_city');
$department->department_phone = $request->input('department_phone');
if($department->save()) {
return new Department($department);
}
}
在 Postmen 中使用相同的参数(但不在 json 中)执行此帖子时,它确实将其插入到数据库中,但我收到以下错误:
“传递给 Illuminate\Database\Eloquent\Model::__construct() 的参数 1 必须是数组类型,给定对象”
【问题讨论】: