Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\main::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/cms.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\main::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
laravel框架查看sql语句方式 - 互联网笔记

略微加速

略速 - 互联网笔记

laravel框架查看sql语句方式

2022-09-16 leiting (2847阅读)

标签 PHP

方式一  直接查看sql和参数:

开启框架查询日志,执行查询,打印查询日志。这种方式 sql和参数不会拼在一起,需要自己拼接参数

//开启执行日志
DB::connection()->enableQueryLog();

//执行查询        
PlanProductGroup::query()->where('plan_id', $plan_id)->where('product_group_id', $product_group_id)->increment($count_type);

//打印sql,和参数
print_r(DB::getQueryLog()); //获取查询语句、参数和执行时间
die;

方式二  占位符绑定参数:

查询转为sql,获取绑定的参数,把占位符用sprintf 拼接成最终sql

$list_DB_sql = str_replace('?', '%s', $list->toSql());

$binding = $list->getBindings();

$list_DB_sql = sprintf($list_DB_sql, ...$binding);

echo $list_DB_sql;die;


北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3