【发布时间】:2022-01-26 06:32:59
【问题描述】:
我已经习惯了,但是今天这个问题让我很虚弱..;;
class Market {
// ..
public function ttl()
{
return $this->ttlRelation()->firstOrCreate(
['market_id' => $this->id],
['tier' => 0, 'direction'=>0]
);
}
}
Market 模型有一个 TTL 模型。我知道firstOrCreate 方法找到一个项目作为第一个给定的数组,如果它不存在创建一个新的作为持久化,返回它。
此外,它的质量分配,所以我在 ttl 模型上填写了$fillable 属性..
class TradingTacticalLayer extends Model
{
public $timestamps = false;
protected $fillable = ['direction', 'tier'];
}
..我收到SQLSTATE[HY000]: General error: 1364 Field 'direction' doesn't have a default value (SQL: insert into "trading_tactical_layer_test" ("tier", "market_id") values (0, 1)) 消息。我不明白为什么这种方法不会以正确的方式填充插入字段列表。我希望,如果我将$fillable 属性编辑为['direction'],SQL 会内爆(“direction”)作为插入字段而它不会。
一般来说,根据我的经验,我只是将这些字段设置为可为空或手动设置默认值。在这个时候,我想知道为什么会发生这种奇怪的事情以及我做错了什么。
【问题讨论】:
-
验证错误是否与您发布的代码有关。
-
你试过
php artisan optimize:clear吗? -
我相信
optimize:clear能以某种方式解决问题。如果你添加答案,我会检查你的。谢谢。
标签: laravel eloquent mass-assignment