首页 > 解决方案 > 解决“违反完整性约束:19 NOT NULL”,最合适的解决方案是什么?

问题描述

尝试编写一个函数来在我的配置文件表中创建一个新的“配置文件”并得到以下错误:

“SQLSTATE [23000]:完整性约束违规:19 NOT NULL 约束失败:profiles.about(SQL:插入“profiles”(“dateofbirth”、“state”、“zipcode”、“profilepic”、“user_id”、“updated_at ", "created_at") 值 (2020-04-15, FL, 12345, /tmp/phpTT6CZr, 1, 2020-04-30 00:48:23, 2020-04-30 00:48:23))”

在过去的几个小时里,我一直在阅读类似问题的答案。尝试了几种不同的东西,到目前为止没有运气。希望看到一个适用于我的代码的解决方案,并更好地了解错误的确切开始位置。该错误消息使我相信这与表中的“关于”部分有关。但不确定。我认为向控制器添加“protected $guarded = [];”可以解决,但结果相同。

这是我正在使用的内容:

迁移文件:

public function up()
{
    Schema::create('profiles', function (Blueprint $table) {
        $table->id();
        $table->unsignedBigInteger('user_id'); //foreign key
        $table->text('about')->nullable;
        $table->text('profilepic')->nullable;
        $table->date('dateofbirth')->nullable;
        $table->unsignedinteger('zipcode')->nullable;
        $table->string('state')->nullable;
        $table->timestamps();

        $table->index('user_id'); //index for foreign key
    });
}

型材型号:

class profile extends Model {


protected $guarded = [];


public function user()
{
    return $this->belongsTo(User::class);
} }

我也尝试过更改配置文件模型,如下所示:

class profile extends Model {

public function user()
{
    return $this->belongsTo(User::class);
}

/**
 * The attributes that should be cast to native types.
 *
 * @var array
*/

protected $casts = [
    'dateofbirth' => 'datetime',
    'zipcode' => 'unsignedinteger'
];

 /*
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $fillable = [
    'about','profilepic','state', 'user_id', 'updated_at', 'created_at'
]; }

它们都提供相同的错误消息,但数组值略有不同

这是我的控制器存储功能:

public function store()
{
    $data = request()->validate([
        'dateofbirth' => 'required',
        'state' => 'required',
        'zipcode' => 'required',
        'profilepic' => 'image'
    ]); 

    auth()->user()->profile()->create($data);

    dd(request()->all());
}

这是视图:

@extends('layouts.app')

@push('styles')
<link href="{{ asset('css/profile.css') }}" rel="stylesheet">
@endpush

@section('content')
{{-- This needs to present a create profile form --}}

<div class="row">
    <h1 class="pl-4">CREATE YOUR PROFILE</h1>
</div>

<form action="/profile" class="pl-4" enctype="multipart/form-data" method="post">
    @csrf

    <div class="form-group row">
        <label for="profilepic"
        class="col-md-4 ocl-form-label"
        >Upload a Profile Picture</label>

        <input type="file"
        class="form-control-file"
        id="profilepic"
        name="profilepic">
    </div>

    <div class="form-group">
        <label for="about">Write your "About" Section here. What do you want us to know about you?</label>
        <textarea type="text" class="form-control" id="about" name="about" rows="3"></textarea>
    </div>

    <div class="form-group">
        <label for="dateofbirth">Date of Birth</label>

        <input type="date"
        id="dateofbirth"
        name="dateofbirth">
    </div>

    <div class="form-group">
        <label for="zipcode">Zipcode</label>
        <input type="text" id="zipcode" name="zipcode">
    </div>

    <div class="form-group">
        <label for="State">State</label>
        <input type="text" id="state" name="state">
    </div>

    <div class="form-group row pt-4">
        <button class="btn btn-primary">Submit</button>
    </div>
</form> @endsection

标签: phpsqllaravelnullablemass-assignment

解决方案


该错误意味着您正在尝试将外键列设置为 null 这是不可接受的,在这种情况下,user_idprofiles表上。尝试修改您的代码:在您的配置文件模型中,添加质量分配列:

protected $fillable = ['dateofbirth', 'state', 'zipcode', 'profilepic'];

在您的控制器存储方法中:

//assuming the route method is authenticated such that there's always a logged in user
$user = auth()->user();
$data['user_id'] = $user->id;
$profile = Profile::create($data);

推荐阅读