【发布时间】:2017-02-02 19:47:14
【问题描述】:
我创建了一个小费游戏,用户可以猜测一个值,12 小时后,API 请求将采用当前的美元/欧元汇率,并且小费最接近值的获胜者将获胜(只有一个获胜者,如果 n 用户小费相同数量的所有人都将获胜)。
如何从数据库中获取“最近”的提示?
foreach ($rounds_closed as $round_closed)
{
//get latest rate
$amount = 458.12;
//set transaction as processed and set latest rate
$round_closed->update([
'status' => 2,
'win_tip' => $amount,
]);
//get lucky winners
//here I would need eloquent code to choose the "nearest" winners
$winners_transactions = Transaction::where('round_id', $round_closed->id)->where('assessment', $round_closed->win_tip)->get();
//if we have at least one winner
if ($winners_transactions->count() > 0)
{
//calculate their win amount
$sum = $round_closed->transactions->sum('amount');
$fee = $sum * 0.10;
$sum = $sum - $fee;
$sum = number_format($sum / $winners_transactions->count(), 8, '.', '');
foreach ($winners_transactions as $winner_transaction)
{
//mark transaction as payed out
$winner_transaction->update([
'payout' => 1,
]);
//payout user
}
}
}
【问题讨论】:
标签: sql orm eloquent laravel-5.2 nearest-neighbor