1. 程式人生 > >laravel 獲取某個查詢的查詢SQL語句

laravel 獲取某個查詢的查詢SQL語句

get con print start spa query 數組 pri gate

一、DB類查詢

DB::connection()->enableQueryLog();     //開啟執行日誌
$count = DB::table(‘test‘)              //執行查詢
->whereNull(‘deleted_at‘)
->where(‘id‘, ‘=‘, 3)
->where(‘Name‘, ‘=‘, ‘測試‘)
->count();
print_r(DB::getQueryLog());             //獲取查詢語句、參數和執行時間

返回值:

Array
(
    [0] => Array
        (
            [query] 
=> select count(*) as aggregate from `test` where `deleted_at` is null and `id` = ? and `Name` = ? [bindings] => Array ( [0] => 3 [1] => 測試 ) [time] => 1 ) )

如果是ORM封裝的語句

$query = CdbForumSellthreadSearch::where($params
)->orderBy("$orderby", "$ascDesc")->offset($start)->limit($limit); $list = CdbForumSellthreadSearch::where($params)->orderBy("$orderby", "$ascDesc")->offset($start)->limit($limit)->get()->toArray(); //打印出的sql是預編譯的sql語句,帶有問號占位符的sql print_r($query->toSql());
echo ‘<pre>‘;

//
下面打印出占位符對應的變量的值,數組裏面的順序和占位符的順序是一致的
print_r($query->getBindings());

laravel 獲取某個查詢的查詢SQL語句