Laravel method to use value of another column as function parameter in Where statement
P粉974462439
P粉974462439 2023-12-27 17:39:22
0
1
508

I have a Round model for a Laravel PHP game. Each round has a start time (DATETIME) and duration in minutes (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

A game will have multiple rounds. If start_time uration > Carbon::now(), one round ends

Now I can't seem to figure out how to retrieve all turns from a game that is still in progress

I thought of something similar, but apparently that doesn't work because I can't put the "duration" column into the subMinutes function

return $game->whereHas('rounds', function ($query) {
            $query->where('start_time', '>', Carbon::now()->subMinutes(duration));
        })->first();


P粉974462439
P粉974462439

reply all(1)
P粉593536104

You are looking for this in SQL:

WHERE start_time > CURRENT_TIMESTAMP - INTERVAL duration SECOND

or equivalent, in Eloquent:

$query->whereRaw(sql: 'WHERE start_time > CURRENT_TIMESTAMP - INTERVAL duration SECOND');
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template