Eloquent: Query Builder Compare Two Date Columns
I regularly share my knowledge about Laravel, Web Development tips, and tutorials via my blog.
Now, here in this article, I am going to cover about comparing two different timestamp columns to filter out records from the database.
Recently I was working on filtering some API activity log to push them on notification interface, and the database design was with columns consisted of timestamp datatype as
I wanted to filter the activity log based on values in two dates columns
optimized_at, my logic was to fetch database records if the timestamp in
synced_at is greater
optimized_at. The API process normally does a remote sync and does some database optimization task for making frontend stuff load faster with a database view.
I want to give you two different ways of comparing the columns.
Activity::where('synced_at', '>', DB::raw('optimized_at')) ->latest() ->get();
Activity::whereRaw('unsynced_at > optimized_at') ->latest() ->get();
Both of the database queries from the above code snippet give same results.
Thanks for reading this post up to the end, if you think this post is worth reading, feel free to share with others, also if you have feedback please post in the comment section below.