获取在 Laravel 中执行的查询 3/4 [英] Get the Query Executed in Laravel 3/4
问题描述
如何使用 Laravel Query Builder 或 Eloquent ORM 在 Laravel 3/4 中检索原始执行的 SQL 查询?
例如,像这样:
DB::table('users')->where_status(1)->get();
或者:
(posts (id, user_id, ...))User::find(1)->posts->get();
否则,至少如何将执行的所有查询保存到 laravel.log?
Laravel 4+
<块引用>Laravel 5 用户注意事项: 在执行查询之前,您需要调用 DB::enableQueryLog()
.要么在运行查询的行上方,要么在中间件内部.
在 Laravel 4 及更高版本中,您必须调用 DB::getQueryLog()
来获取所有已运行的查询.
$queries = DB::getQueryLog();$last_query = end($queries);
或者您可以下载分析器包.我推荐 barryvdh/laravel-debugbar,它非常简洁.您可以在他们的存储库中阅读有关如何安装的说明.
Laravel 3
在 Laravel 3 中,您可以从 Eloquent
模型调用 DB
类上的静态方法 last_query
获取最后执行的查询.
DB::last_query();
然而,这要求您在 application/config/database.php
中启用 profiler
选项.或者,正如@dualed 提到的,您可以在 application/config/application.php
中启用 profiler
选项或调用 DB::profile()
获取当前请求中运行的所有查询及其执行时间.
How can I retrieve the raw executed SQL query in Laravel 3/4 using Laravel Query Builder or Eloquent ORM?
For example, something like this:
DB::table('users')->where_status(1)->get();
Or:
(posts (id, user_id, ...))
User::find(1)->posts->get();
Otherwise, at the very least how can I save all queries executed to laravel.log?
Laravel 4+
Note for Laravel 5 users: You'll need to call
DB::enableQueryLog()
before executing the query. Either just above the line that runs the query or inside a middleware.
In Laravel 4 and later, you have to call DB::getQueryLog()
to get all ran queries.
$queries = DB::getQueryLog();
$last_query = end($queries);
Or you can download a profiler package. I'd recommend barryvdh/laravel-debugbar, which is pretty neat. You can read for instructions on how to install in their repository.
Laravel 3
In Laravel 3, you can get the last executed query from an Eloquent
model calling the static method last_query
on the DB
class.
DB::last_query();
This, however, requires that you enable the profiler
option in application/config/database.php
. Alternatively you could, as @dualed mentioned, enable the profiler
option, in application/config/application.php
or call DB::profile()
to get all queries ran in the current request and their execution time.
这篇关于获取在 Laravel 中执行的查询 3/4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!