简体   繁体   English

使用Laravel获取记录时遇到问题

[英]Having trouble getting records using Laravel

I have the following MySQL query: 我有以下MySQL查询:

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. 当我在MySQL中运行该命令时,它为我提供了过去一天的记录数。

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. 我正在Laravel中尝试执行此操作,因为它包含在我正在使用的WHMCS软件中,并且无法真正理解如何使其工作。

Sometimes, when you are stuck in Laravel, just paste your SQL into a raw query like 有时,当您陷入Laravel时,只需将SQL粘贴到原始查询中,例如

$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();

您可以像$ query = DB :: table('your table')-> whereDate('date','> =',Carbon :: today())-> whereDate('date','<=' ,Carbon :: tomorrow())-> count();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM