Estoy haciendo un siple with y un with count con las mismas condiciones, el with me lo hace perfecto pero el withCount lo hace mal,estoy usando lumen este es mi codigo
$query =Employe::query();
$query->withCount([
'staffEdps' => function ($staff_edp) use ($date, $rec, $stationConfig, $recertificationMonth, $stationsIdHomologateMe) {
$staff_edp
->orderBy('end_date', 'desc')->where('turn_stretch_id', $rec->turn_stretch_id)
->where(function ($q) use ($date) {
$q->where('init_date', '>=', $date)
->where('end_date', '<=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('init_date', '>=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('staff_edp.init_date', '<', $date)
->where('staff_edp.end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('staff_edp.init_date', '<', $date)
->where('staff_edp.end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('staff_edp.init_date', '<=', $date)
->whereNull('staff_edp.end_date');
})->where('configuration_station_id', $rec->configuration_station_id);
$staff_edp->whereHas('datesActive', function ($datestaff) use ($date) {
$datestaff->where(function ($q) use ($date) {
$q->where('init_date', '>=', $date)
->where('end_date', '<=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('init_date', '>=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('init_date', '<=', $date)
->whereNull('end_date');
});
});
}
]);
$query->with('staffEdps', function ($staff_edp) use ($rec, $date) {
$staff_edp
->where('turn_stretch_id', $rec->turn_stretch_id)
->orderBy('end_date', 'desc')
->where(function ($q) use ($date) {
$q->where('init_date', '>=', $date)
->where('end_date', '<=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('init_date', '>=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('staff_edp.init_date', '<', $date)
->where('staff_edp.end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('staff_edp.init_date', '<', $date)
->where('staff_edp.end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('staff_edp.init_date', '<=', $date)
->whereNull('staff_edp.end_date');
});
$staff_edp->withAndWhereHas('datesActive', function ($datestaff) use ($date) {
$datestaff->where(function ($q) use ($date) {
$q->where('init_date', '>=', $date)
->where('end_date', '<=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('init_date', '>=', $date);
})
->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('init_date', '<', $date)
->where('end_date', '>', $date);
})->orWhere(function ($q) use ($date) {
$q->where('init_date', '<=', $date)
->whereNull('end_date');
});
})->where('configuration_station_id', $rec->configuration_station_id);
});
$query->orderBy('staff_edps_count', 'desc');
El with y el with count estan haciendo los mismos filtros pero el withcount cuenta mas de lo que es, ya lo probe con whereHas y con WithAndWHereHas