我有一個(gè) Laravel PHP 遊戲的 Round 模型。每回合都有一個(gè)開始時(shí)間 (DATETIME) 和以分鐘為單位的持續(xù)時(shí)間 (INT):
id | game_id | duration | start_time 1 | 3 | 40 | 2022-06-22 19:29:26 2 | 3 | 20 | 2022-06-24 00:02:55 3 | 1 | 10 | 2022-06-25 10:56:05
一場(chǎng)比賽會(huì)有多輪,如果 start_time uration > Carbon::now()
則一輪結(jié)束
現(xiàn)在我似乎不知道如何從仍在進(jìn)行的遊戲中檢索所有回合
我想到了類似的事情,但顯然這不起作用,因?yàn)槲覠o法將「duration」列放入 subMinutes 函數(shù)中
return $game->whereHas('rounds', function ($query) { $query->where('start_time', '>', Carbon::now()->subMinutes(duration)); })->first();
您正在 SQL 中尋找此內(nèi)容:
WHERE start_time > CURRENT_TIMESTAMP - INTERVAL duration SECOND
或相同的,在 Eloquent 中:
$query->whereRaw(sql: 'WHERE start_time > CURRENT_TIMESTAMP - INTERVAL duration SECOND');