Laravel 5,使用UUID时,无法获取刚刚保存模型的ID
Laravel 5, can't get the ID of the just saved model, when using UUID
我正在为我的帖子 table 使用 Laravel Eloquent,并为我的 table 的主键使用 UUID。但是保存模型后我无法从中获取 ID,尽管所有值都已正确插入数据库。
$post = new App\Post();
$post->uuid = \Webpatser\Uuid\Uuid::generate();
$post->save();
dd($post->uuid); //return null or 0
添加访问器或使用 $post->getKey()
无法解决我的问题。
根据 Laravel API 文档,Eloquent 模型有一个布尔值 属性 $incrementing
指示 ID 是否自动递增。要在我们的模型中使用 UUID,我们必须像这样将此变量设置为 false:
public $incrementing = false;
我正在为我的帖子 table 使用 Laravel Eloquent,并为我的 table 的主键使用 UUID。但是保存模型后我无法从中获取 ID,尽管所有值都已正确插入数据库。
$post = new App\Post();
$post->uuid = \Webpatser\Uuid\Uuid::generate();
$post->save();
dd($post->uuid); //return null or 0
添加访问器或使用 $post->getKey()
无法解决我的问题。
根据 Laravel API 文档,Eloquent 模型有一个布尔值 属性 $incrementing
指示 ID 是否自动递增。要在我们的模型中使用 UUID,我们必须像这样将此变量设置为 false:
public $incrementing = false;