Level 75
Jun 18, 2022
4
Level 1
Laravel eluquent - whereBetween date_of_birth
I'm trying to filter data where the date_of_birth is between the min_age and max_age.
But the date of birth is saved in the database like " 1973-05-17 " and the min_age/max_age is for example 18 and 25. Is there a simple way so I can make the max_age and min_age to a date with only this data?
// this is what the user gives to filter
$this->user_filters->min_age = 18;
$this->user_filters->max_age = 26;
->whereBetween('date_of_birth', [$this->user_filters->min_age, $this->user_filters->max_age])
Level 122
You need to work out the date range for those two ages based on today's date
$youngest = today()->subYears(18);
$oldest = today()->subYears(26);
->whereBetween('date_of_birth',[$oldest, $youngest])
1 like
Please or to participate in this conversation.