Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 毕竟,laravel可以记录sql查询吗?_Php_Sql_Laravel_Logging_Query Builder - Fatal编程技术网

Php 毕竟,laravel可以记录sql查询吗?

Php 毕竟,laravel可以记录sql查询吗?,php,sql,laravel,logging,query-builder,Php,Sql,Laravel,Logging,Query Builder,在illumb/Database/Connection.php中,我可以看到: /** * Indicates whether queries are being logged. * * @var bool */ protected $loggingQueries = false; /** * Enable the query log on the connection. * * @return void */

illumb/Database/Connection.php
中,我可以看到:

/**
 * Indicates whether queries are being logged.
 *
 * @var bool
 */
protected $loggingQueries = false;

/**
 * Enable the query log on the connection.
 *
 * @return void
 */                                                                                                                                              public function enableQueryLog()
{
    $this->loggingQueries = true;
}

/**
 * Disable the query log on the connection.
 *
 * @return void
 */
public function disableQueryLog()
{
    $this->loggingQueries = false;
}

/**
 * Determine whether we're logging queries.
 *
 * @return bool
 */
public function logging()
{
    return $this->loggingQueries;
}

/**
 * Run a SQL statement and log its execution context.
 *
 * @param  string    $query
 * @param  array     $bindings
 * @param  \Closure  $callback
 * @return mixed
 *
 * @throws \Illuminate\Database\QueryException
 */
protected function run($query, $bindings, Closure $callback)
{
    ...

    $this->logQuery($query, $bindings, $time);

    return $result;
}

/**
 * Log a query in the connection's query log.
 *
 * @param  string  $query
 * @param  array   $bindings
 * @param  float|null  $time
 * @return void                                                                                                                                   */
public function logQuery($query, $bindings, $time = null)                                                                                        {
    ...

    if ($this->loggingQueries) {                                                                                                                         $this->queryLog[] = compact('query', 'bindings', 'time');
    }
}
这些数据是否存储在某个地方?如果是,我如何全局启用它?

我将使用它来启用它。这将允许您使用选项启用(如仅在
本地
环境中,等等)

您可以使用
dd()
或使用
Log::info()
等方法收集信息。例如:

namespace App\Http\Middleware;

use DB;
use Log;
use Closure;

class EnableQueryLogMiddleware
{
    public function handle($request, Closure $next)
    {
        if (env('local')) {
            DB::enableQueryLog();
        }

        return $next($request);
    }

    public function terminate($request, $response)
    {
        // Here you can either Log it, DD, etc.
        dd(DB::getQueryLog());
        Log::info(DB::getQueryLog());
    }
}
我将使用它来启用它。这将允许您使用选项启用(如仅在
本地
环境中,等等)

您可以使用
dd()
或使用
Log::info()
等方法收集信息。例如:

namespace App\Http\Middleware;

use DB;
use Log;
use Closure;

class EnableQueryLogMiddleware
{
    public function handle($request, Closure $next)
    {
        if (env('local')) {
            DB::enableQueryLog();
        }

        return $next($request);
    }

    public function terminate($request, $response)
    {
        // Here you can either Log it, DD, etc.
        dd(DB::getQueryLog());
        Log::info(DB::getQueryLog());
    }
}

所以没有简单的方法,比如在
config/database.php
中指定一些参数?我觉得创建上述中间件很容易,需要<5分钟才能启用。对我来说,这是最简单的方法,但这只是我的观点。您可以使用
DB::enableQueryLog()
应用程序中的任意位置,这使您能够记录查询,因此理论上,您可以将其放在
app/bootstrap/app.php
文件中。不优雅,但我相信它会起作用。只是确认一下。我希望,只需更改一些配置参数就可以使用它。但事实证明,这需要更多的努力。不过不多。我可能同意使用中间件通常是一个更好的选择。是的,我同意将它作为
config/database.php
中的一个参数是很好的,但在这一点上似乎不可能。也许在未来的版本中!所以没有简单的方法,比如在
config/database.php
中指定一些参数?我觉得创建上述中间件很容易,需要<5分钟才能启用。对我来说,这是最简单的方法,但这只是我的观点。您可以使用
DB::enableQueryLog()
应用程序中的任意位置,这使您能够记录查询,因此理论上,您可以将其放在
app/bootstrap/app.php
文件中。不优雅,但我相信它会起作用。只是确认一下。我希望,只需更改一些配置参数就可以使用它。但事实证明,这需要更多的努力。不过不多。我可能同意使用中间件通常是一个更好的选择。是的,我同意将它作为
config/database.php
中的一个参数是很好的,但在这一点上似乎不可能。也许在未来的版本中!