laravel框架查看sql语句方式

方式一  直接查看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;

 

posted @ 2021-04-28 16:19  礼物de绷带  阅读(515)  评论(0编辑  收藏  举报