如何获取查询生成器以字符串形式输出其原始SQL查询?

本文翻译自:How Do I Get the Query Builder to Output Its Raw SQL Query as a String?

Given the following code: 给出以下代码:

DB::table('users')->get();

I want to get the raw SQL query string that the database query builder above will generate. 我想获取上面的数据库查询生成器将生成的原始SQL查询字符串。 In this example, it would be SELECT * FROM users . 在此示例中,它将是SELECT * FROM users

How do I do this? 我该怎么做呢?


#1楼

参考:https://stackoom.com/question/1EW5m/如何获取查询生成器以字符串形式输出其原始SQL查询


#2楼

To output to the screen the last queries ran you can use this: 要将最近运行的查询输出到屏幕,可以使用以下命令:

DB::enableQueryLog(); // Enable query log

// Your Eloquent query

dd(DB::getQueryLog()); // Show results of log

I believe the most recent queries will be at the bottom of the array. 我相信最近的查询将在数组的底部。

You will have something like that: 您将拥有类似的东西:

array(1) {
  [0]=>
  array(3) {
    ["query"]=>
    string(21) "select * from "users""
    ["bindings"]=>
    array(0) {
    }
    ["time"]=>
    string(4) "0.92"
  }
}

(Thanks to Joshua's comment below.) (感谢约书亚在下面评论。)


#3楼

You can listen to the 'illuminate.query' event. 您可以听“ illuminate.query”事件。 Before the query add the following event listener: 在查询之前,添加以下事件侦听器:

Event::listen('illuminate.query', function($query, $params, $time, $conn) 
{ 
    dd(array($query, $params, $time, $conn));
});

DB::table('users')->get();

This will print out something like: 这将打印出类似以下内容:

array(4) {
  [0]=>
  string(21) "select * from "users""
  [1]=>
  array(0) {
  }
  [2]=>
  string(4) "0.94"
  [3]=>
  string(6) "sqlite"
}

#4楼

Use the toSql() method on a QueryBuilder instance. QueryBuilder实例上使用toSql()方法。

DB::table('users')->toSql() would return: DB::table('users')->toSql()将返回:

select * from `users` 从用户中选择*

This is easier than wiring up an event listener, and also lets you check what the query will actually look like at any point while you're building it. 这比连接事件侦听器容易,而且还使您可以在构建查询时随时查看查询的实际外观。


#5楼

If you are trying to get the Log using Illuminate without Laravel use: 如果您尝试使用不带Laravel的Illuminate获取日志,请使用:

\Illuminate\Database\Capsule\Manager::getQueryLog();

You could also nock up a quick function like so: 您也可以像这样添加一个快速功能:

function logger() {
    $queries = \Illuminate\Database\Capsule\Manager::getQueryLog();
    $formattedQueries = [];
    foreach( $queries as $query ) :
        $prep = $query['query'];
        foreach( $query['bindings'] as $binding ) :
            $prep = preg_replace("#\?#", is_numeric($binding) ? $binding : "'" . $binding . "'", $prep, 1);
        endforeach;
        $formattedQueries[] = $prep;
    endforeach;
    return $formattedQueries;
}

EDIT 编辑

updated versions seem to have query logging disabled by default (the above returns an empty array). 默认情况下,更新的版本似乎已禁用查询日志记录(以上返回空数组)。 To turn back on, when initialising the Capsule Manager, grab an instance of the connection and call the enableQueryLog method 要重新打开,请在初始化Capsule Manager时获取连接的实例并调用enableQueryLog方法

$capsule::connection()->enableQueryLog();

EDIT AGAIN 再次编辑

Taking the actual question into consideration, you could actually do the following to convert the current single query instead of all previous queries: 考虑到实际问题,您实际上可以执行以下操作来转换当前的单个查询而不是所有先前的查询:

$sql = $query->toSql();
$bindings = $query->getBindings();

#6楼

您可以使用此包获取加载页面时正在执行的所有查询

https://github.com/barryvdh/laravel-debugbar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值