在Laravel 4中使用DB :: insert()返回新ID [英] Return new id with DB::insert() in Laravel 4

查看:960
本文介绍了在Laravel 4中使用DB :: insert()返回新ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Laravel 4中,当您执行DB::insert()时,如何获取刚刚插入的行的ID?与功能->insertGetId()类似.使用DB::insert()的原因是它是一个复杂的PostgreSQL查询,无法使用Fluent构建.

In Laravel 4, when you perform a DB::insert(), how can you get the ID of the row that has just been inserted? Similar what we have with the function ->insertGetId(). Reason for using DB::insert() is that its a complex PostgreSQL query that cannot be built using Fluent.

示例查询:

$newId = DB::insert('insert into users (id, name) values (?, ?)', array(1, 'Dayle'));
echo $newId; // returns 1

推荐答案

有一个insertGetId方法.

There is a insertGetId method.

$id = DB::table('users')->insertGetId(
    array('id' => 1, 'name' => 'Dayle Rees')
);

这篇关于在Laravel 4中使用DB :: insert()返回新ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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