【问题标题】:Returning primary key from Laravel 5.2 DB on raw query在原始查询中从 Laravel 5.2 DB 返回主键
【发布时间】:2016-05-11 04:16:07
【问题描述】:

在原始查询插入的情况下,有没有办法返回主键?

我有:

$sql = "insert into table(field1, field2) select (field1+1), ".$id." from table where field2=".$id." order by field3 desc limit 0,1";
$return = (DB::insert(DB::raw($sql)));

我的 $return 是一个布尔值,所以在插入后不进行选择,我可以得到这个 PK 吗?

【问题讨论】:

标签: php mysql laravel eloquent


【解决方案1】:

我想在DB::insert 调用之后没有办法检索 PK/最后插入的 id。

正如您在Illuminate\Database\ConnectionInterface 中看到的,insert 方法返回一个布尔值,因为它是从Illuminate\Database\Connection 中的PDOstatement::excecute 返回的。

最终你在DB::insert 之后使用DB::getPdo()->lastInsertId()

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 2018-01-26
    • 2015-01-08
    • 1970-01-01
    • 2021-09-18
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多