更改模型中的Banner.php
~~~
<?php
namespace app\api\model;
use think\Db;
use think\Model;
class Banner
{
public static function getBannerById($id)
{
//$result=Db::query('select * from banner_item where banner_id=?',[$id]);
$result=Db::table('banner_item')
->fetchSql()
->where(
function ($query) use ($id){
$query->where('banner_id','=',$id);
}
)->select();
return $result;
}
}
~~~
1.打印sql语句,如上代码,在链式结构中加入`fetchSql()`,
![](https://box.kancloud.cn/778c1f7fa7acdbd594a052baccc42727_849x510.png)
2.在index.php中加入日志初始化代码,初始化级别为sql
```
\think\Log::init([
'type' => 'File',
'path' => LOG_PATH,
'level' => ['sql']
]);
```
运行之后便会产生日志文件
![](https://box.kancloud.cn/e8055d361f043b4811c542b7b2b0582a_1418x487.png)