我正在尝试制作一个条形图,其中包含用户信用额度的进展情况。我将信用更改存储在一个包含两列old_amount和new_amount的表中。它还有一个名为time_added的列。
我想如果我能抓住当天的最后一个并获得new_amount,它会给我当天用户的信用计数。
$playersCredits7daysAgo = CashChanges::where('user_id', $player->id)->orderBy('time_added', 'DESC')->take(1)->new_amount;
$playersCredits6daysAgo = CashChanges::where('user_id', $player->id)->orderBy('time_added', 'DESC')->take(1)->new_amount;
$playersCredits5daysAgo = CashChanges::where('user_id', $player->id)->orderBy('time_added', 'DESC')->take(1)->new_amount;
$playersCredits4daysAgo = CashChanges::where('user_id', $player->id)->orderBy('time_added', 'DESC')->take(1)->new_amount;
$playersCredits3daysAgo = CashChanges::where('user_id', $player->id)->orderBy('time_added', 'DESC')->take(1)->new_amount;
$playersCredits2daysAgo = CashChanges::where('user_id', $player->id)->orderBy('time_added', 'DESC')->take(1)->new_amount;
$playersCreditsYesterday = CashChanges::where('user_id', $player->id)->orderBy('time_added', 'DESC')->take(1)->new_amount;
$playersCreditsToday = $player->credits;
正如你可以看到我的查询几乎有效,我如何做一个where子句以确保它在我需要它的那一天内?并且不会从不同的日子中选择一个吗?
我将时间添加到名为time_added的列中,格式为:2017-03-04 21:05:58
答案 0 :(得分:0)
您可以使用雄辩的whereDate
方法
$playersCredits7daysAgo = CashChanges::where('user_id', $player->id)->whereDate('time_added', Carbon::today()->subDays(7))->first()->new_amount;
或者如果您想在一个查询中执行
CashChanges::where('user_id', $player->id)->where('time_added', '>', Carbon::today()->subDays(7))->pluck('new_amount', 'time_added');
这将返回new_amount
和time_added
数组作为键