Laravel 无法在 eloquent 创建事件时设置 ID

Laravel cannot set id on eloquent creating event

我正在尝试为我的模式提供一个自定义 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

您需要通过在您的模型中将 属性 $incrementing 设置为 false 来禁用自动递增。

public $incrementing = false