Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use paginator::make() in laravel to show resultset in view?

I have used Paginator::make to paginate the records in the table. In the view, I am getting the pagination links, but every link has all the records in it. How to restrict it to perPage items?

    $datas = Paginator::make($paginator, count($paginator), $perPage);
    return $datas;

The code outputs:

{"total":10,"per_page":5,"current_page":1,"last_page":2,"from":1,"to":5,"data":      
[{"id":"10","languages":"ds","created_at":"2014-05-23 
11:59:02.000","created_by":"1","updated_at":"2014-05-23 
11:59:02.000","updated_by":"1","is_active":"1"},
{"id":"9","languages":"urdu","created_at":"2014-05-23 
11:57:24.000","created_by":"1","updated_at":"2014-05-23 
11:57:24.000","updated_by":"1","is_active":"1"},
{"id":"8","languages":"were","created_at":"2014-05-23 
11:55:49.000","created_by":"1","updated_at":"2014-05-23 
11:55:49.000","updated_by":"1","is_active":"1"},  
{"id":"7","languages":"delete","created_at":"2014-05-23  
11:54:57.000","created_by":"1","updated_at":"2014-05-24  
06:02:46.000","updated_by":"1","is_active":"1"},
{"id":"6","languages":"sdf","created_at":"2014-05-23 
11:53:11.000","created_by":"1","updated_at":"2014-05-23 
11:53:11.000","updated_by":"1","is_active":"1"},
{"id":"5","languages":"dada","created_at":"2014-05-23 
11:51:33.000","created_by":"1","updated_at":"2014-05-24 
05:44:34.000","updated_by":"1","is_active":"1"},
{"id":"4","languages":"English","created_at":"2014-05-23 
11:49:49.000","created_by":"1","updated_at":"2014-05-23 
11:49:49.000","updated_by":"1","is_active":"1"},
{"id":"3","languages":"asdfgf","created_at":"2014-05-23 
11:48:20.000","created_by":"1","updated_at":"2014-05-23 
11:48:20.000","updated_by":"1","is_active":"1"},
{"id":"2","languages":"Tamil","created_at":"2014-05-23 
10:55:50.000","created_by":"1","updated_at":"2014-05-23 
10:55:50.000","updated_by":"1","is_active":"1"},
{"id":"1","languages":"Tamil","created_at":"2014-05-23 
10:51:42.000","created_by":"1","updated_at":"2014-05-26 
04:41:27.000","updated_by":"1","is_active":"1"}]}
like image 288
uma Avatar asked May 26 '14 12:05

uma


2 Answers

Actually Paginator::make function we need to pass only the required values instead of all values. Because paginator::make function simply displays the data send to it. To send the correct offset paginated data to the paginator::make, the following method should be followed

    $paginator = json_decode($response);
    $perPage = 5;   
    $page = Input::get('page', 1);
    if ($page > count($paginator) or $page < 1) { $page = 1; }
    $offset = ($page * $perPage) - $perPage;
    $articles = array_slice($paginator,$offset,$perPage);
    $datas = Paginator::make($articles, count($paginator), $perPage);

Hope this will help someone...

like image 118
uma Avatar answered Nov 19 '22 04:11

uma


Paginator::make()

This function only creates the paging system. To restrict your data for each page you have to do some changes to your query. Here is an example.

        $pageNo = Input::get('page', 1);
        $perPage = 10;
        $from = $pageNo*$perPage-$perPage;
        $to = $perPage;

        $data['allData'] = DB::select( DB::raw('SELECT * FROM tablename LIMIT '.$from.','.$to));
        $totalData = DB::select( DB::raw('SELECT * FROM tablename'));
        $data['paginator'] = Paginator::make($data['allData'], count($totalData), $perPage);

Then you can show the data to your view page

like image 3
BlackJack Avatar answered Nov 19 '22 03:11

BlackJack