类class之前记得引用use Illuminate\Support\Facades\DB;
在方法里面这样干
DB::connection()->enableQueryLog();#开启执行日志
$product = $product->where('product_first_category_id', '=', $val['id'])->where('product_status', '=', 20);
$result = $product->with([
'images' => function ($query) {
$query->where('is_delete', '=', 10)->where('is_default', '=', 1);
},
'product_spec' => function ($query) {
}
])->offset(0)->limit($limit)->orderBy('id', 'desc')->get()->toArray();
print_r(DB::getQueryLog());//打印SQL语句
结果如下:
Array ( [0] => Array ( [query] => select * from `product` where `is_delete` = ? and `product_first_category_id` = ? and `product_status` = ? order by `id` desc limit 6 offset 0 [bindings] => Array ( [0] => 10 [1] => 1 [2] => 20 ) [time] => 0.62 ) [1] => Array ( [query] => select * from `product_images` where `product_images`.`product_id` in (?, ?, ?) and `is_delete` = ? and `is_default` = ? [bindings] => Array ( [0] => 8 [1] => 10 [2] => 12 [3] => 10 [4] => 1 ) [time] => 0.47 ) [2] => Array ( [query] => select * from `product_spec` where `product_spec`.`product_id` in (?, ?, ?) [bindings] => Array ( [0] => 8 [1] => 10 [2] => 12 ) [time] => 0.44 ) )