Mysql procedure with pagination in laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this blog I will show you how to mysql procedure with pagination in laravel. When we are working on laravel with mysql database. we need to created mysql procedure for get data and we want to call procedure also with pagination in laravel  then we cannot directly make pagination as laravel document. But if we are using directly MySQL stored Procedure in our controller, model or repository and we want to give pagination as like this way :

    $data = DB::select(DB::raw('CALL srs("hari")'))->paginate(5);   

So on this way we found an error in our Laravel website. We can’t give it directly this way pagination because our procedure will get all the data from the database. but we can give pagination this way :

$page = Input::get('page', 1);  
    $paginate = 2;  
      
    $data = DB::select(DB::raw('CALL srs("hari")'));  
      
    $offSet = ($page * $paginate) - $paginate;  
    $itemsForCurrentPage = array_slice($data, $offSet, $paginate, true);  
    $data = new \Illuminate\Pagination\LengthAwarePaginator($itemsForCurrentPage, count($data), $paginate, $page);  
      
    return view('test',compact('data'));  

Read Also : How to Open Link in New Tab on Click in javascript ?

Thanks for read this, I hope it will you. You can also follow us on Facebook.

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →