【发布时间】:2020-08-14 06:39:09
【问题描述】:
试图编写一个函数来在我的配置文件表中创建一个新的“配置文件”并得到以下错误:
“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
【问题讨论】:
标签: php sql laravel nullable mass-assignment