Laravel开发记录sql

啊 这瞌睡 这瞌睡像十除以三

函数

1
2
3
4
5
6
7
8
9
10
11
function trace_sql($dump = false)
{
DB::listen(function ($event) use($dump) {
if ($dump) {
dump($event->sql);
dump($event->bindings);
}
Log::info($event->sql);
Log::info($event->bindings);
});
}

使用

1
2
3
4
5
6
7
8
9
10
11
12
public function index()
{
trace_sql();
$query = Order::query();
//$query->whereBetween('updated_at',[Carbon::today(), Carbon::tomorrow()]);
$query->whereHas('incomeExpenditure', function ($query) {
$query->whereBetween('created_at',[Carbon::today(), Carbon::tomorrow()])->where('value', '>', 0);
});
$orders = $query->where('status', 'paid')->with('products')->orderBy('updated_at', 'desc')->paginate(1000);
$datas = $this->todayStatistics();
return view('home.index', ['orders' => $orders, 'datas' => $datas]);
}