Advertisement
rodro1

custom Pagination With Collection data laravel api

Jan 3rd, 2022
20
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.56 KB | None | 0 0
  1. public function popularNews($lan){
  2. $this->lan = $lan;
  3.  
  4.  
  5. $query = TblPost::query();
  6.  
  7. $query->inRandomOrder();
  8.  
  9. $perPage = 9;
  10. $total = $query->count();
  11. $page = request()->input('page', 1);
  12.  
  13. $query->orderBy('id', 'DESC');
  14.  
  15. $data = $query->offset(($page - 1) * $perPage)->limit($perPage)->get();
  16.  
  17. return [
  18. 'data' => $this->getCollections($data),
  19. 'total' => $total,
  20. 'page' => $page,
  21. 'last_page' => ceil($total / $perPage)
  22. ];
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement