打印sql語句,直接在你執行SQL語句後輸出php
方法一:html
$queries = DB::getQueryLog(); $a = end($queries); $tmp = str_replace('?', '"'.'%s'.'"', $a["query"]); echo vsprintf($tmp, $a['bindings']); exit;
方法二: 能夠把下面代碼放在查詢語句前:laravel
\DB::listen(function($sql, $bindings, $time) { foreach ($bindings as $replace){ $value = is_numeric($replace) ? $replace : "'".$replace."'"; $sql = preg_replace('/\?/', $value, $sql, 1); } dd($sql); })
方法三:git
下載 clockwork
擴展,這個擴展能夠在不少框架裏調試,好比laravel
,lumen
,CI
等等,非常好用,github
安裝完之後,直接在firebug
裏能夠看到執行的語句!app
方法四:框架
執行ide
php artisan make:listener QueryListenerpost
會生成app/Listeners/QueryListener.php
文件
而後把handler
修改爲下面這樣
namespace App\Listeners; use Illuminate\Database\Events\QueryExecuted; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Contracts\Queue\ShouldQueue; class QueryListener { /** * Create the event listener. * * @return void */ public function __construct() { // } public function handle(QueryExecuted $event) { $sql = str_replace("?", "'%s'", $event->sql); $log = vsprintf($sql, $event->bindings); \Log::info($log); } }
打開 app/Providers/EventServiceProvider.php
,在 $listen
中添加
protected $listen = [ 'App\Events\SomeEvent' => [ 'App\Listeners\EventListener', ], 'Illuminate\Database\Events\QueryExecuted' => [ 'App\Listeners\QueryListener' ] ];
而後在 本身的storage\log\
下看本身的日誌吧!
相似這樣
[2017-01-02 02:50:09] local.INFO: select count(*) as aggregate from `g9zz_posts` [2017-01-02 02:50:09] local.INFO: select * from `g9zz_posts` limit 30 offset 0 [2017-01-02 02:50:09] local.INFO: select * from `g9zz_categories` where `g9zz_categories`.`id` in ('1', '6', '5', '3', '4') [2017-01-02 02:50:09] local.INFO: select * from `g9zz_users` where `g9zz_users`.`id` in ('8', '12', '10', '16', '5') [2017-01-02 02:50:09] local.INFO: select * from `g9zz_users` where `g9zz_users`.`id` in ('11', '17', '0')
daily
的,否則日誌大小會爆炸的哦(config/app.php 裏的APP_LOG
)