Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AbehoM's avatar

whereYear min and max age

I'm trying to create two where clauses to get the min and max age of an user, I'm trying like this:

    public function minAge($minAge)
    {
        $date = today()->subYears($minAge);

        return $this->whereYear('birthday', '>=', $date);
    }

    public function maxAge($maxAge)
    {
        $date = today()->subYears($maxAge);

        return $this->whereYear('birthday', '<=', $date);
    }

I have in my database an user with the birthday of 1975-02-06 (44 years old), when I call minAge(20) it should return the user but it doesn't. Calling maxAge(44) (or bellow) works, did I do something wrong?

0 likes
10 replies
Nakov's avatar

So you are comparing 1975 >= 1999 and how do you expect this to result to true?

Where in the other case you are comparing 1975 <= 1975 which of course is true hence the result given.

AbehoM's avatar

I mean, creating the max age is kinda easy, I haven't figured out how to do the min age tho.

Nakov's avatar

Well you need the max age code to achieve both.. just different params.

Or just do this instead:

public function ageInterval($min, $max)
{
    $startDate = today()->subYears($min);
    $endDate = today()->subYears($max);

    return $this->whereBetween('birthday', [$startDate, $endDate]);
}

Then simply calling ageInterval(44, 20) should give you the group of users between those two dates.

AbehoM's avatar

The thing is that I'm receiving these parameters as query, it's pretty much a form with min_age and max_age so I get something like ?min_age=20&max_age=40 and that is why I needed the methods separated. I'm using https://github.com/Tucker-Eric/EloquentFilter to create the filters.

Nakov's avatar

Then as I said above, you need the same code to achieve both. I am not familiar with the package and I am answering from a phone, so I cannot look into it now.

AbehoM's avatar

@nakov Sorry, I'm really confused, I don't get what you mean with I need the same code to archive both. The page is just a library that makes model filterable, the idea behind it is to get all the request query and filter the model, for example, if I create a method called minAge inside the filter (this filter is associated with the model), if I pass in the url the query ?min_age=20 it will automatically call the minAge method in the filter and perform the clause inside before retrieving from the database. So in this case I have two filter methods for minAge and maxAge each gets the query value from the request and I need to perform the query based on that.

Nakov's avatar
Nakov
Best Answer
Level 73

@zefex what I mean is that you need the same check for both methods, ie this one:

return $this->whereYear('birthday', '<=', $date);
AbehoM's avatar

You are right, it worked, it's so weird, I can't think straight at 4AM in the morning. Thank you very much, you are a life savior.

AbehoM's avatar

@nakov I added a new entry in the database with the age of 25 (1994) and tried: ?min_age=20&max_age=44 and well, it didn't work, the 25 years old user doesn't show.

Nakov's avatar

@zefex if you try to think better after you sleep you will see why it won't work :) a 25 is not both bigger than 20 and 44 but it is in the middle. So you will need to find a way to use whereBetween because that's the most appropriate one, or in the maxAge function you will have to use

return $this->orWhereYear('birthday', '<=', $date);

so the logic is it is more than 20 OR more than 44 but not both..

Please or to participate in this conversation.