LaravelLaravl 5.7

Best Practices for Laravel Date Time Queries

Best Practices for Laravel Date Time Queries

Views

Hello to all, welcome to therichpost.com. In this post, I am sharing the tricks for, Best Practices for Laravel Date Time Queries.

I am sharing all these because I was not aware that laravel gives us these kind of date time queries to get rid of date time custom functions and this thing, I like very much and that is why laravel makes things easy to understandable. 

I just share that code, which I do or I think, this could be important in future.

 

Here are the those wonderful Date Time Laravel Queries: 

 

1. The whereDate method may be used to compare a column’s value against a date:

$users = DB::table('users')
                ->whereDate('created_at', '2016-12-31')
                ->get();

 

2. The whereMonth method may be used to compare a column’s value against a specific month of a year:

$users = DB::table('users')
                ->whereMonth('created_at', '12')
                ->get();

 

3. The whereDay method may be used to compare a column’s value against a specific day of a month:

$users = DB::table('users')
                ->whereDay('created_at', '31')
                ->get();

 

4. The whereYear method may be used to compare a column’s value against a specific year:

$users = DB::table('users')
                ->whereYear('created_at', '2016')
                ->get();

 

5. The whereTime method may be used to compare a column’s value against a specific time:

$users = DB::table('users')
                ->whereTime('created_at', '=', '11:20:45')
                ->get();

 If have any query related this, then do comment or ask questions.

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Positive SSL