平时使用PHP+MySQL开发的时候经常会用到获取刚刚插入的记录的id,这是可以直接使用mysql自带的函数LAST_INSERT_ID()
获取,但是现在使用Laravel进行开发的,原来laravel也是支持获取刚刚插入的记录id这一功能的,方法如下:
方法一:
insertGetId
示例:
orderid=DB::table( this->table)->insertGetId([‘order_sn’ =>ordersn,′userid′=> user_id, ‘order_price’ =>ordertotalprice,′orderpoint′=> god_goods_total_price, ‘address_id’ =>addressid,′ordermessage′=> order_message, ‘add_time’ => time()]);
$order_id = Order::insertGetId(['order_sn' => $order_sn, 'user_id' => $user_id, 'order_price' => $pay_price, 'order_point' => $pay_point, 'address_id' => $address_id, 'order_type' => $order_type, 'order_message' => $order_message, 'add_time' => time(), 'invoice' => $invoiceInfo['invoice'], 'invoice_type' => $invoiceInfo['invoice_type'], 'invoice_title_type' => $invoiceInfo['invoice_title_type'], 'invoice_title' => $invoiceInfo['invoice_title']]);
方法二:
insert_get_id
注:第二种方法是网上找的,但是我用的时候却失败,不知道是不是版本不一样造成的,我的是最新的版本Laravel5.0。
Author:leedaning
本文地址:http://blog.csdn.net/leedaning/article/details/47006127