Home  >  Q&A  >  body text

Laravel method to use value of another column as function parameter in Where statement

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粉974462439P粉974462439271 days ago441

reply all(1)I'll reply

  • P粉593536104

    P粉5935361042023-12-28 11:43:13

    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');

    reply
    0
  • Cancelreply