jeudi 22 décembre 2022

Laravel Eloqunet query on first relation of one to many

I have Two Tables A and B A can have more than B the relation between them is that B has A_id columns in B have A_id and date I have a filter with status (status alpha = first B of A date is less than 30 days,beta = first B of A date is less than 60) when trying to get the status I want to query on the first row in B which has A_id

public function alpha(){
    return $this->hasOne("App/B")->where('date','<',now()->subMonth(1)->toDateString())->where('date','>',now()->subMonth(2)->toDateString());
}
public function beta(){
    return $this->hasOne("App/B")->where('date','<',now()->subMonth(2)->toDateString());
}

This seems to get the job done but failed in specific situation imagine table B has two rows

  1. id = 1 A_id=1 and date is 45 days old
  2. id =2 A_id=1 and date is 70 days old

This should be an alpha since I'm only interested in the First date but the query I've written returns on both alpha and beta how can I fix my query to only check the first occurrence only mind that I can't loop over A's after getting them since I'm using pagination



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire