Php SQLSTATE[HY000]:一般错误:1364字段';联系人ID';不';没有默认值
在我的型号(Php SQLSTATE[HY000]:一般错误:1364字段';联系人ID';不';没有默认值,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,在我的型号(Customer)上,我有一个必需的参数:$contactId 执行Customer::create(['contactId'=>$contactId])时,我收到以下错误: “消息”:“SQLSTATE[HY000]:一般错误:1364字段“contactId”没有默认值(SQL:insert intocustomers(在更新,在创建)值(2019-12-10 12:33:462019-12-10 12:33:46))” insert语句中找不到contactId字段。conta
Customer
)上,我有一个必需的参数:$contactId
执行Customer::create(['contactId'=>$contactId])
时,我收到以下错误:
“消息”:“SQLSTATE[HY000]:一般错误:1364字段“contactId”没有默认值(SQL:insert intocustomers
(在
更新,在
创建)值(2019-12-10 12:33:462019-12-10 12:33:46))”
insert语句中找不到contactId字段。contactId
的值设置为4
,该值通过FK
与数据库中的正确值相对应
客户
表的迁移:
`架构::创建('customers',函数(Blueprint$表){
$table->bigIncrements('id');
$table->timestamp('created_at')->默认值(DB::raw('CURRENT_timestamp');
$table->timestamp('updated_at')->默认值(DB::raw('CURRENT_timestamp on update CURRENT_timestamp');
$table->bigInteger('contactId')->unsigned();
$table->foreign('contactId','FK_customer\u contactId\u contact\u id'))
->引用('id')
->关于(“联系人”);
});`
是什么阻止了雄辩的在此处创建正确的insert语句?在整个流程和数据库中,我已经三次检查了contactId
的拼写
当我在客户
中手动插入一行时,检索数据和联系人
-关系没有问题
谢谢你的时间 在客户
型号中设置contactId$filleble
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['contactId'];
}
如果使用创建
方法将数据保存到数据库中,则必须在模型中写入此可填充
属性
<?php
class Customer extends Model
{
protected $fillable = ['contactId'];
}
使contactId$可在模型中填充
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $fillable = ['contactId'];
}
是$contactId
一个空值吗?@JeremyHarris它不可为空。原来我没有完全理解$fillable的工作原理。迪利普:他的答案是正确的。
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Customer extends Model
{
protected $guarded = [];
}