Laravel 查看SQL执行记录

利用监听器
php artisan make:listener QueryListener --event=Illuminate\\Database\\Events\\QueryExecuted

vi app/Listeners/QueryListener.php
public function handle(QueryExecuted $event)
{
    if (env('APP_ENV', 'production') == 'local') {
        $sql = str_replace("?", "'%s'", $event->sql);
        $log = vsprintf($sql, $event->bindings);
        Log::info($log);
    }
}

vi app/Providers/EventServiceProvider.php
protected $listen = [
    ...
    \Illuminate\Database\Events\QueryExecuted::class => [
        \App\Listeners\QueryListener::class,
    ],
];
其他
1:
$results = ModelArticle::query()->where(['id' => 1])->toSql();
dd($results);

2:
\DB::listen(function($sql, $bindings, $time) {
    var_dump($sql);
    var_dump($bindings);
    var_dump($time);
});

3:
Install Laravel Debugbar
https://github.com/barryvdh/laravel-debugbar


ps: Laravel Eloquent ORM where 多条件查询
$goodsShow = Goods::where('cate_id','=',$cate_id)
    ->where(function($query){
        $query->where('status','<','61')
            ->orWhere(function($query){
                $query->where('status', '91');
            });
    })->first();