Laravel where age clause

laravel orwhere
laravel eloquent join
and condition in where clause in laravel
laravel subquery
laravel db::query
laravel raw query with parameters
simple select query in laravel
laravel between

I have a Student model and it has properties such as full_name, birthdate, ... birthdate is a date type column.

how can I filter my students and find those students with more than 20 years old?

something like:

$students = Student::whereAge('birthdate', '>=', 20)->get();

You cannot use whereAge as Age should be a column that exists in your database in that case.

You can try this instead:

$students = Student::whereYear('birthdate', '>=', now()->subYears(20)->year)->get();

Even better maybe to compare the exact date 20 years ago:

$students = Student::whereDate('birthdate', '>=', now()->subYears(20))->get();

AGE range using Carbon and Eloquent, I want to find some people by AGE range. parameter) you could add a basic WHERE clause; if both parameters are specified and different,  Laravel's database query builder provides a convenient, fluent interface to creating and running database queries. It can be used to perform most database operations in your application and works on all supported database systems. The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.


Using raw SQL:

$students = Student::whereRaw('TIMESTAMPDIFF(YEAR, birthdate, CURDATE()) >= 20'); 

Database: Query Builder - Laravel, Parameter Grouping; Where Exists Clauses; Subquery Where Clauses; JSON Using the select method, you can specify a custom select clause for the query: Laravel 5.3 - whereDate (), whereMonth (), whereDay () and whereYear () examples. Laravel is a very popular PHP framework. Laravel 5.3 release few days ago and there are several new feature added by Laravel. Laravel 5.3 introduce several new where conditions like whereDate (), whereMonth () etc in Query Builder.


You can remove -20 years from today's date, and then compare it to the birthdate. Like so:

$date = date('Y-m-d', strtotime(date('Y-m-d').' -20 year'));

$students = Student::whereDate('birthdate', '<', $date)->get();

Let me know if this helped.

Query Builder - Laravel, Adding A Select Clause To An Existing Query If you would like to use a "where" style clause on your joins, you may use the where and orWhere methods on a  WHERE ((gender = ‘Male’ and age >= 18) or gender = ‘Female’) and age >= 65. above returns male or female above 64, the 18 becomes useless because you are stating AND age is above 65 outside of the nest. all this is doing is blocking the query from considering any male under 19, which it does anyway.


Lesson 8: Advanced Where and When, Meaning, we'll need a parenthesized, two-part where clause that in sql would look like: WHERE age < 6 OR (age > 8 AND name IN ('Jane', 'Jerry')) {tip} If your global scope is adding columns to the select clause of the query, you should use the addSelect method instead of select. This will prevent the unintentional replacement of the query's existing select clause.


AND-OR-AND + brackets with Eloquent, WHERE (gender = 'Male' and age >= 18) or (gender = 'Female' and Where Clauses – where we can assign a function to a where clause, like  The filter validator, which uses PHP's filter_var function under the hood, ships with Laravel and is Laravel's pre-5.8 behavior. The dns and spoof validators require the PHP intl extension.


Laravel 5 Essentials, Eloquent also ships with a feature-rich query builder that allows you to build If we have a more open WHERE clause, where we were expecting potentially  Blade is the simple, yet powerful templating engine provided with Laravel. Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application.