2014-06-21 40 views
0

在我的app/config/app.php文件中,我打開了'debug'=>true。 我試圖Laravel 4:如何顯示SQL查詢運行?

//run eloquent functions 
$allMessages = Messages::with('User')->whereIn('conv_id',$conv_id)->orderBy('created_at','aadesc')->take(10); 

$q= DB::getQueryLog(); 
dd($q); 

但它返回一個空數組。所以我猜想按照建議做end($q)是沒用的。

我也嘗試了可接受的answer to a question並將其添加到我的路線文件的末尾,但沒有任何發生。我對Laravel仍然很陌生,需要一些指導。謝謝!

回答

2

一種流行的方法是監測口才事件,並輸出任何疑問在數據庫上運行,你沿着打算:

Event::listen('illuminate.query', function($query, $params, $time, $conn) 
{ 
    dd(array($query, $params, $time, $conn)); 
}); 

$allMessages = Messages::with('User')->whereIn('conv_id',$conv_id)->orderBy('created_at','aadesc')->take(10); 

將輸出時運行的查詢。

另一種選擇是使用Laravel4調試器包,它會自動顯示你的查詢運行:https://github.com/barryvdh/laravel-debugbar

+0

謝謝!欣賞它。 :) – Mark