在 Eloquent 查询中使用 Carbon
Using Carbon in Eloquent Query
大家好,
我目前正在实现一个系统,让同事填写他们的工作时间和他们的工作内容。我使用 table 中命名的 time_stamp(日期)将它们保存到数据库中。现在我一直在尝试获取上周填写的注册值(我创建了一些虚拟时间)。我一直在尝试同时使用 Carbon 和 Eloquent 查询生成器,但我完全被卡住了。有人愿意帮我吗?
$currentDate = \Carbon\Carbon::now('GMT+2');
$agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();
$weekly = Hoursregistration::pluck('date')->agoDate($currentDate);
return $weekly;
是应该从数据库中获取日期的代码(有效)。但是当我尝试放入包含碳方法的变量时。它不起作用,并向我抛出一个方法 agoDate 不存在。 (查看:/var/www/clients/client0/web319/web/resources/views/hoursregistrations/index.blade.php)错误。
我希望能得到一些帮助,因为这对我的教育至关重要(有点紧张 rn。)
如您所求:从 1 week ago
到 now
的所有 Hoursregistration
条记录
// Current date + GMT(+2) as stated in your question
$currentDate = Carbon::now('GMT+2');
// Date exactly 1 week ago
$agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();
// Records with date -between- two values
// $weekly = Hoursregistration::whereBetween('date', [$agoDate, Carbon::now('GMT+2')])->get();
// Or even simpler, all records where date is 'higher' than 1 week ago
$weekly = Hoursregistration::where('date', '>', $agoDate)->get();
// Getting the dates with the `pluck` method on the returned $weekly collection
$dates = $weekly->pluck('date');
return $weekly;
大家好,
我目前正在实现一个系统,让同事填写他们的工作时间和他们的工作内容。我使用 table 中命名的 time_stamp(日期)将它们保存到数据库中。现在我一直在尝试获取上周填写的注册值(我创建了一些虚拟时间)。我一直在尝试同时使用 Carbon 和 Eloquent 查询生成器,但我完全被卡住了。有人愿意帮我吗?
$currentDate = \Carbon\Carbon::now('GMT+2');
$agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();
$weekly = Hoursregistration::pluck('date')->agoDate($currentDate);
return $weekly;
是应该从数据库中获取日期的代码(有效)。但是当我尝试放入包含碳方法的变量时。它不起作用,并向我抛出一个方法 agoDate 不存在。 (查看:/var/www/clients/client0/web319/web/resources/views/hoursregistrations/index.blade.php)错误。
我希望能得到一些帮助,因为这对我的教育至关重要(有点紧张 rn。)
如您所求:从 1 week ago
到 now
Hoursregistration
条记录
// Current date + GMT(+2) as stated in your question
$currentDate = Carbon::now('GMT+2');
// Date exactly 1 week ago
$agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();
// Records with date -between- two values
// $weekly = Hoursregistration::whereBetween('date', [$agoDate, Carbon::now('GMT+2')])->get();
// Or even simpler, all records where date is 'higher' than 1 week ago
$weekly = Hoursregistration::where('date', '>', $agoDate)->get();
// Getting the dates with the `pluck` method on the returned $weekly collection
$dates = $weekly->pluck('date');
return $weekly;