我需要根据laravel 6 中的withcount((函数按记录排序
$query=User::withCount(['time_spent' =>function($q){
$q->select(DB::raw('COALESCE(sum(COALESCE(time_spent, 0 )),0)'))
->whereDate("created_at", ">=", Carbon::today()->startOfMonth()->toDateString())
->whereDate("created_at", "<=", Carbon::today()->endOfMonth()->toDateString());
}])
->withCount(['interactive_time_spent' =>function($q){
$q->select(DB::raw('COALESCE(sum(COALESCE(audio_video_time, 0 ) + COALESCE(chat_time,0)),0)'))
->whereDate("created_at", ">=", Carbon::today()->startOfMonth()->toDateString())
->whereDate("created_at", "<=", Carbon::today()->endOfMonth()->toDateString());
}])
->orderBy("(interactive_time_spent_count + time_spent_count)",$sortOder)->get();
在这段代码中,我有两个withCount((函数,我需要在get((之前根据这两列的总和对By进行排序。当使用一列排序时,它可以工作,但如果我使用两列,那么它会返回一个未知列。这可能吗?
在这种情况下,您需要将orderByRaw
用于自定义表达式
->orderByRaw("(interactive_time_spent_count + time_spent_count) ".$sortOder)