For my Log
model in a Laravel application, I am trying to select all records that are "in sequence within a certain diff". What I mean with that is selecting all consequetive logs as long as for the given datetime column the difference between the previous and the next record is within the given time difference limit.
See the query below with some made-up syntax to try to get my intentions across.
Log::query()
->whereDateDiff('created_at', '<', '2 hours') // This method does not exist.
->get();
So suppose I have the following data. Here I would like to select the records up to and including row 4, since the difference in time between row 4 and 5 is bigger than 2 hours.
----------------------------
| id | created_at |
----------------------------
| 1 | 2022-11-17 08:01:52 |
| 2 | 2022-11-17 08:31:14 |
| 3 | 2022-11-17 09:02:49 |
| 4 | 2022-11-17 09:03:30 |
| 5 | 2022-11-17 16:21:12 |
| 6 | 2022-11-17 17:01:27 |
----------------------------
I have searched the internet for an Eloquent approach, but I did not find a way to define some kind of query yet. Learning from this Stack Overflow question ("Date Difference between consecutive rows"), we know how to calculate the difference between consecutive rows in SQL in the SELECT statement, but I would like to have this in the WHERE. I hope this is possible within one query, preferably using an Eloquent syntax, but I am afraid this is might be only possible within the PHP layer. What is the best approach for this problem?