How to make queries to large tables in Laravel
You should avoid using SQL functions in your Laravel Eloquent queries because they inflict full table scans. This happens due to database mechanisms – the condition is not applied until the date function has been run.
// ❌ You could it like this
$posts = Post::whereDate('created_at', '>=', now() )->get();
It will produce a query like this:
select * from posts where date(created_at) >= 'timestamp-here'
The better way:
// ✅ You should do it like this
$posts = Post::where('created_at', '>=', now() )->get();
So we get this exact query, without the date()
function:
select * from posts where created_at >= 'timestamp-here'