withCount([ // 'assets as active_devices_count' => function ($query) { // $query->whereHas('devices', function ($q) { // $q->where('active', 1); // }); // }, // 'assets as inactive_devices_count' => function ($query) { // $query->whereHas('devices', function ($q) { // $q->where('active', 0); // }); // } // ]) // ->where('id', $token['sub']) // ->first(); // return response()->json($userAssetLinks); $token = readHeaderToken(); $userAssetLinks = UserAssetLink::with(['user', 'asset.devices']) ->withCount([ 'asset as active_devices_count' => function ($query) { $query->whereHas('devices', function ($q) { $q->where('active', 1); }); }, 'asset as inactive_devices_count' => function ($query) { $query->whereHas('devices', function ($q) { $q->where('active', 0); }); } ]) ->get(); return response()->json($userAssetLinks) ->withHeaders([ 'Authorization' => "Bearer $token" ]); } }