Sherwin Flight
Sherwin Flight

Reputation: 2363

Having trouble getting records using Laravel

I have the following MySQL query:

SELECT COUNT(id) FROM `tblname` WHERE date >= CURDATE() - INTERVAL 1 DAY;

When I run that in MySQL it gives me the number of records for the past day.

I'm trying to do this in Laravel, because it's included in the WHMCS software I'm using, and can't really understand how to make it work.

Upvotes: 0

Views: 44

Answers (2)

Rashi Goyal
Rashi Goyal

Reputation: 941

You can use carbon like $query = DB::table('your table')->whereDate('date', '>=', Carbon::today())->whereDate('date', '<=', Carbon::tomorrow())->count();

Upvotes: 0

EddyTheDove
EddyTheDove

Reputation: 13259

Sometimes, when you are stuck in Laravel, just paste your SQL into a raw query like

$result = DB::select("SELECT COUNT(id) FROM `tblname` WHERE date >= CURDATE() - INTERVAL 1 DAY");

Using a query builder

$result = DB::table('tblname')
->whereDate('date', '>=', Carbon::today())
->whereDate('date', '<=', Carbon::tomorrow())
->count();

Upvotes: 1

Related Questions