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?
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.
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.
@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 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.
@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