在Eloquent查询中使用Carbon

时间:2017-05-01 13:55:20

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

大家好日子,

我目前正在实现一个系统,可以让同事填写他们的工作时间和他们的工作。我使用表中指定的time_stamp(日期)将它们保存到数据库中。现在我一直试图获得上周填写注册的值(我创建了一些虚拟时间)。我一直试图同时使用Carbon和Eloquent查询构建器,而且我完全陷入困境。有人会介意帮我吗?

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

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

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

代码应该从db(它可以工作)中获取日期。但是当我尝试输入含碳方法的变量时。它不起作用,并抛出一个方法,之前的日期不存在。 (查看:/var/www/clients/client0/web319/web/resources/views/hoursregistrations/index.blade.php)错误。

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

1 个答案:

答案 0 :(得分:1)

根据您的要求:从Hoursregistration1 week ago

的所有now条记录
// 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;