->whereNull('')// count统计结果集为空的字段值 ->whereNotNull('')// count统计结果集不为空的字段值 ->with(['users']) ->where(function ($query) use ($request) { if ($request->filled('user')) { if($request->get('user') == 0) { $query->whereDoesntHave('users');//关联为空的数据[] } else { $query->whereHas('users');//已关联用户 } } }) ->select([ 'apply.*', DB::raw('IF(isnull(notice_count), 0, notice_count) as notice_count') ]) ->leftJoin( DB::raw('(select count(*) as notice_count, apply_id from notice group by apply_id) as notice'), 'notice.apply_id', '=', 'apply.id' ) -&