Reputation: 117
I am doing filter script. User write age_to and I get users with maximum age. The problem is that my in my users table is only full date of birth. How can I get all data? I think intval function is good but I do not know how to use it in mysql query.
$user = User::with('user_x')->whereHas('user_x', function($query) use ($request) {
$birth_date = Carbon::now()->subYears($request->age_to)->toDateString();
return $query->where('date_of_birth', '<=', $birth_date);
})->get();
Upvotes: 0
Views: 699
Reputation: 23001
Intval probably won't do what you want, but Carbon
may. Since you're going for age, then use Carbon's date setters:
$birth_date = Carbon::now()->subYears($request->age_from)->toDateString(); // Sets the birth date to today's date minus the requested years
Then you can use it in your query:
$user = User::with('user_x')->whereHas('user_x', function($query) use ($birth_date) {
return $query->where('date_of_birth', '<=',$birth_date);
})->get();
Upvotes: 1