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

查看:19
本文介绍了在 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天全站免登陆