违反完整性约束:1048列“ school_id”不能为空

时间:2019-12-04 22:40:43

标签: json laravel vue.js post

当我尝试以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');

以及类似的Controller方法:

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中不使用)执行此Post时,确实会将其插入到db中,但是出现以下错误:

  

“传递给Illuminate \ Database \ Eloquent \ Model :: __ construct()的参数1必须是数组类型,给定对象”

1 个答案:

答案 0 :(得分:0)

我相信它是因为您在给n <- 1 df %>% group_by(group) %>% summarise_at(vars(V1,V2,V3), ~ sum((. * (threshold >= 1/2))^n)) 赋予school_id之前要对其进行更新

Department

还要确保您的Department模型具有以下内容

public function storeLocation(request $request) {

    $department = Department::create($request->all());

    return $department;
}

即使在创建部门之后,您提供的代码也会返回一个新部门。

编辑:

尝试将您的帖子修改为以下内容

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'school_id',
        'department_name',
        'department_address',
        'department_zip',
        'department_city',
        'department_phone',
    ];