正在解决"Integrity constraint violation: 19 NOT NULL",最合适的解决方案是什么?

Resolving "Integrity constraint violation: 19 NOT NULL", what's the most proper solution?

尝试编写函数在我的配置文件 table 中创建一个新的 "profile" 并出现以下错误:

"SQLSTATE[23000]: Integrity constraint violation: 19 NOT NULL constraint failed: profiles.about (SQL: insert into "个人资料" ("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))" =16=]

过去几个小时我一直在阅读类似问题的答案。尝试了几种不同的东西,到目前为止没有运气。希望看到一个适用于我的代码的解决方案,并更好地了解错误的确切开始位置。错误消息让我相信它与我在 table 中的 "about" 部分有关。但不确定。我认为将“ 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

该错误意味着您试图将外键列设置为 null,即 unacceptable,在本例中,user_id on profiles table。尝试这样修改您的代码: 在您的配置文件模型中,添加质量分配列:

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);

我还要补充一点,我已经通过实施@djunehor 的回答解决了这个问题。但有助于解决问题的一件事是将其添加到控制器中:

public function store(Request $request)

起初我并没有像这样传递请求并将其保存到变量中,但是这一步似乎对我 运行 遇到的错误产生了很大的影响。

起初我只是这样做的:

public function store()