Calculate the offset and the number of rows per page and pass it to the stored procedure. You may also need such things as the max page in your logic but that's up to you.
Jan 9, 2017
6
Level 6
pagination for query builder and stored procedure
i can't seem to figure out how to get pagination to work on query builder with stored procedure. it returns an array and not a collection so i can't use ->paginate(). i found this post but i don't know how to incorporate it to my code http://itsolutionstuff.com/post/mysql-procedure-with-pagination-in-laravel-5example.html.
here's what i have.
controller
public function index()
{
$data['results'] = DB::select('SET NOCOUNT ON; EXEC stored_procedure');
return view('reports.index', $data);
}
view
@foreach ($results as $result)
{{ blah blah blah }}
@endforeach
the code above returns a result and i can loop through them as usual. i just need to paginate them.
Level 4
@w1n78 Sure you can, try this:
public function index()
{
$page = request('page', 1);
$pageSize = 10;
$results = DB::select('SET NOCOUNT ON; EXEC stored_procedure');
$offset = ($page * $pageSize) - $pageSize;
$data = array_slice($results, $offset, $pageSize, true);
$paginator = new \Illuminate\Pagination\LengthAwarePaginator($data, count($data), $pageSize, $page);
return view('reports.index', ['results' => $paginator]);
}
1 like
Please or to participate in this conversation.