ItSolutionStuff.com

Mysql procedure with pagination in laravel?

By Hardik Savani • November 5, 2023
Laravel MySql

You are working on laravel with mysql database. you had created mysql procedure for get data and you want to call procedure with pagination in laravel 5 then you can not directly make pagination as laravel document.If you are using directly MySQL stored Procedure in your controller, model or repository and you want to give pagination like this way :

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

This way you found error in your Laravel 5 website. we can't give directly this way pagination because your procedure will get all the data from database. but we can give pagination this way :

$page = Input::get('page', 1);

$paginate = 2;

$data = DB::select(DB::raw('CALL hardik("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'));

Try this........

Tags:
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Add Pagination in Laravel?

Read Now →

How to Add Query String Automatically on Laravel Pagination Links?

Read Now →

Laravel Custom Pagination View Example

Read Now →

Laravel 5 manual pagination with array example

Read Now →

How to Add Pagination with Union in Laravel?

Read Now →