Laravel 5 Pagination with Query Builder

前端 未结 5 1931
情歌与酒
情歌与酒 2021-01-05 10:18

I\'m making a Laravel Pagination based from my query result and be rendered in my view. I\'m following this guide http://laravel.com/docs/5.1/pagination but I get an error:<

5条回答
  •  萌比男神i
    2021-01-05 10:45

    in the doc at this page: http://laravel.com/docs/5.1/pagination we can see that we are not forced to use eloquent.

    $users = DB::table('users')->paginate(15);
    

    but, be sure you don't make a groupBy in your query because, the paginate method uses it.

    after i'm no sure you can use paginate with query builder ( select($query) )

    --- edit

    You can create collection an use the paginator class :

    $collection = new Collection($put_your_array_here);
    
    // Paginate
    $perPage = 10; // Item per page
    $currentPage = Input::get('page') - 1; // url.com/test?page=2
    $pagedData = $collection->slice($currentPage * $perPage, $perPage)->all();
    $collection= Paginator::make($pagedData, count($collection), $perPage);
    

    and in your view just use $collection->render();

提交回复
热议问题