试图在我的个人资料表中编写一个用于创建新“个人资料”的函数并出现以下错误:
“ 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
该错误表示您试图将外键列设置为null,这在user_id
表上的profiles
中是不可接受的。尝试这样修改您的代码:在您的Profile模型中,添加质量分配列: