Laravel save()方法后无法检索自定义ID主键 [英] custom id primary key cannot be retrieved after Laravel save() method

查看:787
本文介绍了Laravel save()方法后无法检索自定义ID主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于具有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:

注意:您可能需要确保Model的incrementingtrue,以便它将尝试从数据库中获取ID.如果您尚未在模型"中设置此字段,请不要进行设置.默认情况下,Model能够在插入后自动从数据库中获取值.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆