Laravel save()方法后无法检索自定义ID主键 [英] custom id primary key cannot be retrieved after Laravel save() method
问题描述
对于具有int的自动增量id,在save()方法之后,它将返回id.
For auto increment id with int, after save() method, it returns id.
但是它不会返回由mysql触发器完成的自定义增量id主键.
But it does not return custom increment id primary key done by mysql trigger.
$productData = new Product($request->all());
$productData->save($productData->getAttributes());
return $productData->id;
在这种情况下如何检索自定义ID?
how to retrieve custom id in this case ?
推荐答案
这超出了laravel,因为数据库提供了自动递增的字段,而laravel无法从数据库中获取last_insert_id.
This is beyond laravel, as auto incremented field is provided by your database and laravel could not get the last_insert_id from your DB.
在这种情况下,您必须更改触发器,以使其将last_insert_id
更改为新值:
For this case you have to change your trigger such that it change last_insert_id
with the new value:
Note: You might need to make sure Model's incrementing
is true
so that it will try to fetch the id from database. If you have not set this field in your Model, then don't set it. By default Model is capable in getting the value from your database automatically after an insert.
这篇关于Laravel save()方法后无法检索自定义ID主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!