I have list of messages by users and I need to get the oldest message of each user, then use paginate()
on the result.
example data:
id | user_id | message | date_posted
1 | 5 | some_message | 2022-07-15
2 | 125 | some_message | 2022-08-02
3 | 5 | some_message | 2022-04-05
So in this case I need to get only rows with id 2
and 3
The problem is that I got this complex query to do it, and I have to use it inside DB::select(DB::raw($query));
, which returns an array, and paginate
can't be used on array.
This is the query:
select T.*
from (select *,
row_number() over (partition by user_id order by date_posted, id) as sn
from my_table
) T
where sn = 1;
Is there a way to get these results with statements that can be converted to Query Builder or Eloquent?
*I can't disable only_full_group_by