[英]SQLSTATE[HY000]: General error: 1364 Field 'photo' doesn't have a default value in laravel 5.5
[英]SQLSTATE[HY000]: General error: 1364 Field 'contactId' doesn't have a default value
在我的模型 ( Customer
) 上,我有一个必需的参数: $contactId
。
在执行Customer::create(['contactId' => $contactId])
我收到以下错误:
"message": "SQLSTATE[HY000]: 一般错误: 1364 字段 'contactId' 没有默认值 (SQL: insert into
customers
(updated_at
,created_at
) 值 (2019-12-10 12:33:46, 2019) -12-10 12:33:46))"
在插入语句中找不到 contactId 字段。 contactId
的值设置为4
,它通过FK
对应于数据库中的正确值。
Customer
表的迁移:
`Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP'));
$table->bigInteger('contactId')->unsigned();
$table->foreign('contactId', 'FK_customer_contactId_contact_id')
->references('id')
->on('contacts');
});`
是什么阻止了Eloquent
在这里创建正确的插入语句? 我已经在整个流程和数据库中三重检查了contactId
的拼写。
当我手动将一行插入到customers
,检索数据和contact
关系没有问题。
谢谢你的时间!
在Customer
模型中使 contactId $fillable
fillable
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['contactId'];
}
如果您使用create
方法将数据保存到数据库中,则必须在模型中编写此fillable
属性
<?php
class Customer extends Model
{
protected $fillable = ['contactId'];
}
在模型中使 contactId $fillable
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['contactId'];
}
或者
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $guarded = [];
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.