Home Laravel Laravel pagination using mysql limit offset

Laravel pagination using mysql limit offset

by therichpost
Published: Updated: 0 comments
Laravel 7.2 routing with route group auth guard check with prefix

Hello to all, welcome to therichpost.com. In this post, i will tell you, Laravel pagination using mysql limit offset.

Laravel is the top mvc framework in php.

Here is working and tested code for Laravel pagination using mysql limit offset:
// Paginate
$limit = 2;

//$_GET['page'] - get page number form url like - www.therichpost.com?page=1

$offset = (null !== $_GET['page']) ? ($_GET['page'] == 1) ? 0 : ($_GET['page']-1) * $limit : 0;

$raw_sql = 'SELECT * FROM user';

//get total number of pages divided by limit
$total_pages = sizeof(DB::select(DB::raw($raw_sql))) / $limit;

$query = 'SELECT * FROM user LIMIT '.$limit.' OFFSET '.$offset;

$user = DB::select(DB::raw($query));

 

 If you have any query related to this post, then feel free to ask. the rich post

 

You may also like

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.