【发布时间】:2019-09-27 09:32:23
【问题描述】:
我正在尝试为我的模式提供一个自定义 ID(无自动增量)。所以我已经覆盖了我的模态的boot 方法。创建事件是这样使用的:
public static function boot()
{
static::creating(function ($modal) {
$modal->id = $myID;
return true;
});
}
现在,当我在保存条目后尝试恢复 ID 时,新条目的 ID 始终为 0。
$modal = new Modal;
$modal->myValue = $myValue;
$modal->save();
dd($modal->id) // This will returns always 0
奇怪的是,记录成功写入了id正确的数据库。
我的代码有什么问题?
编辑:
它没有返回null。它正在返回0
【问题讨论】:
-
试试这个:$modal->getKey();
-
也是
null。 -
您的 id 可以填写吗?
-
你需要添加这个:public $incrementing = false;
-
请发表您的想法作为答案。所以你可以赢得你的声誉:)
标签: php laravel eloquent laravel-6