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::
// 只能接受一個(gè)參數(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)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
下拉列表多級(jí)聯(lián)動(dòng)dropDownList示例代碼
本文為大家詳細(xì)介紹下下拉列表多級(jí)聯(lián)動(dòng) dropDownList具體的實(shí)現(xiàn)代碼,感興趣的朋友可以參考下哈,至于一些細(xì)節(jié)部分后續(xù)再補(bǔ)2013-06-06
Zend Framework動(dòng)作助手FlashMessenger用法詳解
這篇文章主要介紹了Zend Framework動(dòng)作助手FlashMessenger用法,分析了動(dòng)作助手FlashMessenger的功能,并結(jié)合實(shí)例形式演示了FlashMessenger的使用技巧,需要的朋友可以參考下2016-03-03
ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作示例
這篇文章主要介紹了ThinkPHP5.0框架使用build 自動(dòng)生成模塊操作,結(jié)合實(shí)例形式分析了thinkPHP5使用build自動(dòng)生成模塊的具體步驟、方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-04-04
laravel5使用freetds連接sql server的方法
這篇文章主要給大家介紹了關(guān)于laravel5使用freetds連接sql server的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
PHP使用第三方即時(shí)獲取物流動(dòng)態(tài)實(shí)例詳解
這篇文章主要介紹了PHP使用第三方即時(shí)獲取物流動(dòng)態(tài),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-04-04
CodeIgniter中實(shí)現(xiàn)泛域名解析
這篇文章主要介紹了CodeIgniter中實(shí)現(xiàn)泛域名解析的方法,需要的朋友可以參考下2014-07-07
PHP實(shí)現(xiàn)把MySQL數(shù)據(jù)庫導(dǎo)出為.sql文件實(shí)例(仿PHPMyadmin導(dǎo)出功能)
這篇文章主要介紹了PHP實(shí)現(xiàn)把MySQL數(shù)據(jù)庫導(dǎo)出為.sql文件實(shí)例(仿PHPMyadmin導(dǎo)出功能),需要的朋友可以參考下2014-05-05

