【问题标题】:Integrity constraint violation: 1048 Column 'school_id' cannot be null违反完整性约束:1048 列“school_id”不能为空
【发布时间】: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 必须是数组类型,给定对象”

【问题讨论】:

    标签: json laravel vue.js post


    【解决方案1】:

    我相信这是因为你在给它一个 school_id 之前更新了一个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',
        ];
    
    

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

    编辑:

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

        const location = this.location;
        fetch('/api/school/'+this.location.school_id+'/location/store', {
            method: 'post',
            body: {
               ...location,
            },
            header: {
                'content-type': 'application/json'
            }
        })
    
    

    【讨论】:

    • 将方法更改为您的代码并添加大量可分配属性。现在我收到此错误:一般错误:1364 Field 'school_id' doesn't have a default value (SQL: insert into department (updated_at, created_at) 值 (2019-12-04 22:57:29 , 2019-12-04 22:57:29)
    • 当你dd($request->all());时会发生什么
    • 它是空的。它返回:[]
    • @rubex01 我已经更新了我的答案,我认为是因为您正在对帖子进行字符串化,它应该仍然是一个对象。
    • 如果标头内容类型仍然是 application/json,我如何将对象作为正文发送。当将对象作为主体时,它表示有效负载是 [object Object]
    猜你喜欢
    • 2018-03-04
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-20
    • 2021-08-16
    • 2019-10-06
    相关资源
    最近更新 更多