当前位置:Gxlcms > 数据库问题 > laravel 获取某个查询的查询SQL语句

laravel 获取某个查询的查询SQL语句

时间:2021-07-01 10:21:17 帮助过:5人阅读

开启执行日志 $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语句

标签:lis   echo   off   asc   返回值   print   arc   span   sele   

人气教程排行