Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

255
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda