自增 id 为 int,save() 方法后返回 id。
但它不返回由 mysql 触发器完成的自定义增量 id 主键。
$productData = new Product($request->all());
$productData->save($productData->getAttributes());
return $productData->id;
在这种情况下如何检索自定义 ID?
最佳答案
这超出了 Laravel,因为自动递增字段是由您的数据库提供的,而 Laravel 无法从您的数据库中获取 last_insert_id。
对于这种情况,您必须更改触发器,使其将 last_insert_id
更改为新值:
Is there a way to get last inserted id of a NON - auto incremented column in MySQL?
注意:您可能需要确保模型的 incrementing
为 true
,以便它会尝试从数据库中获取 ID。如果您尚未在您的模型中设置此字段,则不要设置它。默认情况下,模型能够在插入后自动从数据库中获取值。
关于php - Laravel save() 方法后无法检索自定义 id 主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48376840/