Laravel Lumen - Eloquent 查询日志
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31704550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Lumen - Eloquent Query Log
提问by ajtrichards
I'm using Laravel Lumen to build an API.
我正在使用 Laravel Lumen 构建 API。
I've come to a point where I need to find out what SQL query is being generated by Eloquent. I know how to do this in Laravel 4 and Laravel 5 but i've tried the same code in Lumen and the query is blank?
我已经到了需要找出 Eloquent 正在生成什么 SQL 查询的地步。我知道如何在 Laravel 4 和 Laravel 5 中执行此操作,但是我在 Lumen 中尝试了相同的代码并且查询为空白?
$queries = DB::getQueryLog();
$last_query = end($queries);
echo 'Query<pre>';
print_r($last_query);
exit;
The above code, when run in Laravel works fine - in Lumen the query is blank?
上面的代码,当在 Laravel 中运行时工作正常 - 在 Lumen 中查询是空白的?
采纳答案by ajtrichards
To get the query log in Laravel Lumen working you need to enable it:
要在 Laravel Lumen 中运行查询日志,您需要启用它:
DB::connection()->enableQueryLog();
DB::connection()->enableQueryLog();
You can add that code in to your controller, middleware, etc then use:
您可以将该代码添加到您的控制器、中间件等中,然后使用:
$queries = DB::getQueryLog();
$lastQuery = end($queries);
dd($lastQuery)
To print your query.
打印您的查询。
You can also use the following with eloquent:
您还可以将以下内容与 eloquent 一起使用:
$myModel = Users::where('active', true);
dd($myModel->getSql(), $myModel->getBindings());
You must run the getSql()
and getBindings()
before you call ->first()
or ->get()
, etc
您必须在调用or等之前运行getSql()
andgetBindings()
->first()
->get()
回答by Mahen Nakar
Just call this after the query to keep it quick and simple:
只需在查询后调用它以保持快速和简单:
echo $query->toSql();