Laravel 5.4:我如何从本周获得记录?

时间:2017-06-10 13:13:06

标签: laravel laravel-5.4

要获取当天的所有记录,我做了

$dt = Carbon::now();
$dataToday = Data::whereDay('created_at', $dt->day)->get();

为了获得本周的记录,我尝试了以下但没有成功。

$dataThisWeek = Data::where('created_at', $dt->weekOfYear);
$dataThisWeek = Data::where('created_at', $dt->weekOfMonth);
$dataThisWeek = Data::whereWeek('created_at', $dt->week);

我们如何使用Eloquent和Carbon(最好)或原生MYSQL函数来解决这个问题?

2 个答案:

答案 0 :(得分:4)

试试这个:

Data::whereBetween('created_at', [Carbon::now()->startOfWeek(), Carbon::now()->endOfWeek()])->get();

设置周开始/结束:

Carbon::setWeekStartsAt(Carbon::SUNDAY);
Carbon::setWeekEndsAt(Carbon::SATURDAY);

答案 1 :(得分:2)

您也可以尝试

declare @DunningNumber int = 600044978 --9 digit code http://www.dnb.com/duns-number.html; so can easily hold in an int: https://docs.microsoft.com/en-us/sql/t-sql/data-types/int-bigint-smallint-and-tinyint-transact-sql

SELECT 
    APPLICATION_ID     
    , nref.value('(./PI_NAME/text())[1]','varchar(max)') PI 
    , nref.value('(./PI_ID/text())[1]','varchar(max)') PI_ID
INTO 
    ADMIN_MUSC_RePORTER_TERMS                   
FROM 
    [ADMIN_Grant_Exporter_Files_XML] 
CROSS APPLY 
    XMLData.nodes('//PIS[../ORG_DUNS/text() = sql:variable("@DunningNumber")]/PI') AS R(nref)
WHERE
    RECID = 1