Laravel Eloquent whereTime Query Example
Laravel 05-Jan-2021

Laravel Eloquent whereTime Query Example

Laravel provides numerous eloquent methods, and you can use different eloquent methods for a different purposes.

But In this laravel tutorial, you will learn about whereTime eloquent method. As well as how to use this eloquent method with laravel query builder and model.

When you develop any application in laravel, and you want to fetch records of specific time only. At that time, you can use laravel whereTime() eloquent method with query.

Here you will see many examples of laravel whereTime method:

Example 1: whereTime method with query builder

$users = DB::table('users')
             ->whereTime('created_at', '=', '09:50:11')
             ->get();
dd($users);

Example 2: whereTime method with eloquent model

$users = User::whereTime('created_at', '=', '09:50:11')
             ->get();
dd($users);

The whereTime query will compare a column’s value against a given specific time in this. And the get result accordingly.

When you dump the above given whereTime query you will get the following SQL query:

select * from `users` where time(`created_at1`) = 09:50:11

And you can also write this whereTime query without operator as follow:

$users = User::whereTime('created_at1', '09:50:11')->get();
dd($users);