Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
Hasmany Relationship where first record is current month in laravel

I have Eloquent with a HasMany relationship. I need to filter only hasmany relationship if the first record is the current month.

Model

       public function attendanceclasses(){
    
            return $this->hasMany('App\Studentattendance','assign_id','id')->WhereIn('attendance_status',[2,3,6]);
    
        }
    
        public function firstattendadclass(){
    
            return $this->hasOne('App\Studentattendance','assign_id','id')->WhereIn('attendance_status',[2,3,6])->OrderBy('class_date','ASC');
        }

Query

                $q->Where('class_date', '>=',date('Y-06-01'));
                $q->Where('class_date','<=',date('Y-m-t'));
            })->get();
over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

You have to filter in `with'. This will filter both parent and child

$firstOfMonth= Carbon::now()->firstOfMonth()->toDateString();
$lastOfMonth=Carbon::now()->lastOfMonth()->toDateString();

$stucourselist=StudentCourses::with(['firstattendadclass'=>function($q)use($firstOfMonth,$lastOfMonth){
    $q->Where('class_date', '>=',$firstOfMonth);
    $q->Where('class_date','<=',$lastOfMonth);
}])->whereHas('firstattendadclass',function($q)use($firstOfMonth,$lastOfMonth){
    $q->Where('class_date', '>=',$firstOfMonth);
    $q->Where('class_date','<=',$lastOfMonth);
})->get();

if you want to filter only child then you can remove wherehas

Also you can use whereBetween('class_date',[$firstOfMonth,$lastOfMonth])

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda