Reputation: 3083
How in laravel 7 app using eloquent to add numerated column for rows, starting from 1 and next 2... for request like :
$forumPosts = ForumPost
::getByForumThreadId($forum_thread_id)
->orderBy($order_by, $order_direction)
->offset($limit_start)
->take($forum_posts_per_page)
->get()
?
Thanks!
Upvotes: 0
Views: 30
Reputation: 12208
try to add counter and select it:
DB::statement(DB::raw('set @rownum='.$limit_start));
$forumPosts = ForumPost
::getByForumThreadId($forum_thread_id)
->orderBy($order_by, $order_direction)
->offset($limit_start)
->take($forum_posts_per_page)
->select('forumPosts.*',DB::raw('@rownum := @rownum + 1 AS rownum'))
->get();
Upvotes: 2