我正在尝试查看查询的日志,但DB::getQueryLog()只是返回一个空数组:
$user = User::find(5);
print_r(DB::getQueryLog());
Run Code Online (Sandbox Code Playgroud)
结果
Array
(
)
Run Code Online (Sandbox Code Playgroud)
如何查看此查询的日志?
在我的数据库中,我有instagram_actions_histories一个表,其中有action_type一个列,在该列中我有不同的数据,例如1或2或3
例如,我正在尝试获取此表数据的关系并对存储在列中的值求和
$userAddedPagesList = auth()->user()->instagramPages()->with([
'history' => function ($query) {
$query->select(['action_type as count'])->whereActionType(1)->sum('action_type');
}
]
)->get();
Run Code Online (Sandbox Code Playgroud)
顺便说一句,这段代码不正确,因为我想获得其中history包含多个的所有内容sum
whereActionType(1)->sum('action_type')
whereActionType(2)->sum('action_type')
whereActionType(3)->sum('action_type')
Run Code Online (Sandbox Code Playgroud)
例如(伪代码):
$userAddedPagesList = auth()->user()->instagramPages()->with([
'history' => function ($query) {
$query->select(['action_type as like'])->whereActionType(1)->sum('action_type');
$query->select(['action_type as follow'])->whereActionType(2)->sum('action_type');
$query->select(['action_type as unfollow'])->whereActionType(3)->sum('action_type');
}
]
)->get();
Run Code Online (Sandbox Code Playgroud)
更新:
$userAddedPagesList = auth()->user()->instagramPages()->with([
'history' => function ($query) {
$query->select('*')
->selectSub(function ($query) {
return $query->selectRaw('SUM(action_type)')
->where('action_type', '=', '1');
}, 'like')
->selectSub(function ($query) {
return $query->selectRaw('SUM(action_type)')
->where('action_type', …Run Code Online (Sandbox Code Playgroud)