Home > Enterprise >  Laravel - How to compare two time fields?
Laravel - How to compare two time fields?

Time:03-30

Database table

Hello There! I want to get only the rows where the punch_in_time is greater than the expected_punch_in_time (You can see the columns in the picture link above). I searched a lot and I did not found anything helpful, I tried the "whereDate" method but it turns out that it only works on datetime fields and not only time as in my case. Any help would be appreciated! Thank you.

CodePudding user response:

You can use whereTime function in querybuilder

whereTime('punch_in_time', '>', 'expected_punch_in_time')

for more details refer link https://laravel.com/docs/9.x/queries

CodePudding user response:

Use eloquent: whereTime (colum, '>' colum);

Or if want pure SQL u can use this:

SELECT * FROM testTime WHERE
CAST(punch_in_time AS TIME) > CAST(expected_punch_in_time AS TIME);

SELECT * FROM testTime WHERE
punch_in_time > expected_punch_in_time;

fiddle here

  • Related