0

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 introducir la descripción de la imagen aquí

0

1 respuesta 1

0

L solución era mas facil de lo que parecia al pasarla a un query sql me di cuenta que las condiciones de fecha no estaban encapsuladas en un solo where como las necesitaba

 $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)
                            ->whereNull('end_date');
                    });
            });

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.