【问题标题】:Using Carbon in Eloquent Query在 Eloquent 查询中使用 Carbon
【发布时间】:2017-09-28 22:30:51
【问题描述】:

大家好,

我目前正在实现一个系统,可以让同事填写他们的工作时间和他们的工作内容。我使用表中命名的时间戳(日期)将它们保存到数据库中。现在我一直在尝试获取上周填写的注册值(我创建了一些虚拟时间)。而且我一直在尝试同时使用 Carbon 和 Eloquent 查询生成器,但我完全被卡住了。有人愿意帮我吗?

$currentDate = \Carbon\Carbon::now('GMT+2');

    $agoDate = $currentDate->subDays($currentDate->dayOfWeek)->subWeek();

    $weekly = Hoursregistration::pluck('date')->agoDate($currentDate);
    return $weekly;

是应该从数据库中获取日期的代码(有效)。但是当我尝试放入包含碳方法的变量时。它不起作用并给我一个 Method agoDate 不存在。 (查看:/var/www/clients/client0/web319/web/resources/views/hoursregistrations/index.blade.php)错误。

我希望得到一些帮助,因为这对我的教育至关重要(有点紧张。)

【问题讨论】:

  • agoDate 不是 Carbon 库中的现有方法 - 您到底想要实现什么?获取日期减去一天?
  • 上周所有记录的项目
  • 自从我的老板告诉我使用 Carbon 来处理日期,以及一个 Eloquent 查询来获取数据

标签: php sql laravel laravel-5 php-carbon


【解决方案1】:

如您所愿:从1 week agonow 的所有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;

【讨论】:

  • 曼利亚姆,你是最棒的。只是……最后一个请求。我想知道如何使用下拉列表(回溯一个月)显示上周的数据。例如选择:4 月 24 日至 5 月 1 日的数据)。如果我能得到一些帮助,那会让我已经悲伤的一天
  • 这有点超出了这个问题的范围,所以在你自己尝试了一下之后,你可能想在 SO 上发布一个新问题。
  • 我今天一直在努力,没有任何进展
  • 在 SO 上发布一个新问题
猜你喜欢
  • 1970-01-01
  • 2019-11-06
  • 2018-07-01
  • 2014-11-02
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 2020-01-31
  • 2017-03-09
相关资源
最近更新 更多