Laravel 5.4 字段没有默认值
Laravel 5.4 field doesn't have a default value
我遇到了这个错误,none 我检查的谷歌搜索结果与我的问题类似。
我有一个包含 class 交易、用户和匹配项的应用程序
一笔交易有很多匹配项。
一个用户有很多匹配项。
一个用户有很多交易。
我正在尝试使用我的 Deal 对象创建一个新的 Match
$deal->matches()->create(['user_id'=>$id]);
这是我的比赛class,我已经定义了所有需要的关系
class Match extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [];
public $timestamps = false;
public $expired_on = "";
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->matched_on = $model->freshTimestamp();
});
}
public function __construct(){
$d = (new \DateTime($this->matched_on))->modify('+1 day');
$this->expired_on = $d->format('Y-m-d H:i:s');
}
/**
* Get the user that owns the match.
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
* Get the deal that owns the match.
*/
public function deal()
{
return $this->belongsTo('App\Deal');
}
}
当我尝试创建新匹配项时,我不断收到此错误。
QueryException in Connection.php line 647:
SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into matches
(deal_id
) values (1))
我守卫的是一个空数组,可能是什么问题?
删除 guarded
数组并添加 fillable
:
protected $fillable = ['user_id', 'deal_id'];
Alexey Mezenin 的答案正确而且很好。
我使用的另一种方法,对于那些想要维护受保护的空数组的人来说,是创建一个新的 Match 对象并放入属性并保存。
$match->user_id = $id;
$match->deal_id = $deal->id;
$match->matched_on = $match->freshTimestamp();
$match->save();
如果您想恢复到以前的行为,请更新您的
config/database.php
文件并为您的连接设置 'strict' => false
。
因为在我的例子中它是一个独特的字段,所以我不能让它为空。
对我来说,我有一个空的构造函数导致了这个问题,不知道为什么。
如果有人知道原因请评论。
public function __construct(){
}
Commenting/removing 它解决了问题。
如果您的模型中有构造函数,只需确保它也调用了父构造函数:
public function __construct( array $attributes = array() ) {
// mandatory
parent::__construct($attributes);
//..
}
否则,它会破坏某些功能,例如 Model::create
。
解决此错误的另一种方法是包含
'strict' => 错误,
进入 config/database.php mysql 数组
我正在使用 Laravel 8 并通过以下两个步骤修复了此错误:
在用户模式下将单词从 $guarded
数组移动到 $fillable
数组
Config.database.php: 'strict' => false
在 'mysql'
的数组中
手动导入/导出数据库时,检查所有table设置的传输是否成功。如果您忘记添加自动递增主键,Laravel 不会为您填充值。
之后添加AUTO_INCREMENT
即可解决问题。
我有这个错误,但我的错误是制作 class 模型:
$book = new Book();
虽然这是真的
$book = new Book($request->all());
更改您的“config/database.php”无济于事。
如果您收到此错误,则说明您没有将数据正确发送到数据库。
检查控制器中的函数,create() 方法可能被 if 语句或其他内容阻止。
或者
如果是 API,请检查来自前端的 post 请求,这就是您的问题所在。
确保表单正确传递到请求中。
我遇到了这个错误,none 我检查的谷歌搜索结果与我的问题类似。
我有一个包含 class 交易、用户和匹配项的应用程序
一笔交易有很多匹配项。 一个用户有很多匹配项。 一个用户有很多交易。
我正在尝试使用我的 Deal 对象创建一个新的 Match
$deal->matches()->create(['user_id'=>$id]);
这是我的比赛class,我已经定义了所有需要的关系
class Match extends Model
{
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $guarded = [];
public $timestamps = false;
public $expired_on = "";
public static function boot()
{
parent::boot();
static::creating(function ($model) {
$model->matched_on = $model->freshTimestamp();
});
}
public function __construct(){
$d = (new \DateTime($this->matched_on))->modify('+1 day');
$this->expired_on = $d->format('Y-m-d H:i:s');
}
/**
* Get the user that owns the match.
*/
public function user()
{
return $this->belongsTo('App\User');
}
/**
* Get the deal that owns the match.
*/
public function deal()
{
return $this->belongsTo('App\Deal');
}
}
当我尝试创建新匹配项时,我不断收到此错误。
QueryException in Connection.php line 647: SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into
matches
(deal_id
) values (1))
我守卫的是一个空数组,可能是什么问题?
删除 guarded
数组并添加 fillable
:
protected $fillable = ['user_id', 'deal_id'];
Alexey Mezenin 的答案正确而且很好。
我使用的另一种方法,对于那些想要维护受保护的空数组的人来说,是创建一个新的 Match 对象并放入属性并保存。
$match->user_id = $id;
$match->deal_id = $deal->id;
$match->matched_on = $match->freshTimestamp();
$match->save();
如果您想恢复到以前的行为,请更新您的
config/database.php
文件并为您的连接设置 'strict' => false
。
因为在我的例子中它是一个独特的字段,所以我不能让它为空。
对我来说,我有一个空的构造函数导致了这个问题,不知道为什么。 如果有人知道原因请评论。
public function __construct(){
}
Commenting/removing 它解决了问题。
如果您的模型中有构造函数,只需确保它也调用了父构造函数:
public function __construct( array $attributes = array() ) {
// mandatory
parent::__construct($attributes);
//..
}
否则,它会破坏某些功能,例如 Model::create
。
解决此错误的另一种方法是包含
'strict' => 错误,
进入 config/database.php mysql 数组
我正在使用 Laravel 8 并通过以下两个步骤修复了此错误:
在用户模式下将单词从
$guarded
数组移动到$fillable
数组Config.database.php:
的数组中'strict' => false
在'mysql'
手动导入/导出数据库时,检查所有table设置的传输是否成功。如果您忘记添加自动递增主键,Laravel 不会为您填充值。
之后添加AUTO_INCREMENT
即可解决问题。
我有这个错误,但我的错误是制作 class 模型:
$book = new Book();
虽然这是真的
$book = new Book($request->all());
更改您的“config/database.php”无济于事。 如果您收到此错误,则说明您没有将数据正确发送到数据库。 检查控制器中的函数,create() 方法可能被 if 语句或其他内容阻止。 或者
如果是 API,请检查来自前端的 post 请求,这就是您的问题所在。 确保表单正确传递到请求中。