最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Laravel5.* 打印出執(zhí)行的sql語句的方法

 更新時間:2017年07月24日 15:22:13   作者:野蠻秘籍  
本篇文章主要介紹了Laravel5.* 打印出執(zhí)行的sql語句的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文介紹了Laravel5.* 打印出執(zhí)行的sql語句的方法,分享給大家,具體如下:

打開app\Providers\AppServiceProvider.PHP,在boot方法中添加如下內(nèi)容

5.2以下版本

// 先引入DB 
use DB;
// 或者直接使用 \DB::
 DB::listen(function($sql, $bindings, $time) {
        dump($sql);
      });

5.2及以上版本

use DB;
// 或者直接使用 \DB::
// 只能接受一個參數(shù)

QueryExecuted {#84 ▼
 +sql: "select * from `posts` where `slug` = ? limit 1"
 +bindings: array:1 [▶]
 +time: 0.59
 +connection: MySqlConnection {#85 ▶}
 +connectionName: "mysql"
}

 DB::listen(function($sql) {
        dump($sql);
        // echo $sql->sql;
        // dump($sql->bindings);
      });

// 如果要放入日志文件中
DB::listen(
  function ($sql) {
    // $sql is an object with the properties:
    // sql: The query
    // bindings: the sql query variables
    // time: The execution time for the query
    // connectionName: The name of the connection

    // To save the executed queries to file:
    // Process the sql and the bindings:
    foreach ($sql->bindings as $i => $binding) {
      if ($binding instanceof \DateTime) {
        $sql->bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
      } else {
        if (is_string($binding)) {
          $sql->bindings[$i] = "'$binding'";
        }
      }
    }

    // Insert bindings into query
    $query = str_replace(array('%', '?'), array('%%', '%s'), $sql->sql);

    $query = vsprintf($query, $sql->bindings);

    // Save the query to file
    $logFile = fopen(
      storage_path('logs' . DIRECTORY_SEPARATOR . date('Y-m-d') . '_query.log'),
      'a+'
    );
    fwrite($logFile, date('Y-m-d H:i:s') . ': ' . $query . PHP_EOL);
    fclose($logFile);
  }
);

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

昭苏县| 宜州市| 九台市| 江永县| 娱乐| 邹平县| 垣曲县| 克东县| 武川县| 靖宇县| 镇原县| 搜索| 民勤县| 镇原县| 新化县| 永清县| 疏附县| 剑川县| 崇明县| 凤庆县| 新民市| 阳朔县| 雷州市| 金秀| 藁城市| 常州市| 虞城县| 安顺市| 公安县| 桐乡市| 弥勒县| 青田县| 姚安县| 荆州市| 慈溪市| 邢台市| 榕江县| 霍州市| 方正县| 若羌县| 佳木斯市|