Search code examples
phplaravel-4datatableserver-side

Data-table server-side pagination with laravel query limit


I am using below query for random number of row in laravel.

Select * from table order by rand() limit 0,1000

And my data-table pagination size is 100 but pagination doesn't working properly when i am using limit in query.

How can i implement this using above query in laravel with server side pagination ?

Thanks!


Solution

  • You should try to make pagination after querying the results, because Laravel add limit automatically. Add below code to your model:

    public function getPaginatorResults($pagination) {
        $total = $this->getTotal();
        $query = $this->orderByRaw('rand()')
                      ->skip($pagination * (\Paginator::getCurrentPage() - 1))
                      ->take($pagination);
        $results = $query->get();
    
        return \Paginator::make($results->all(), $total, $pagination);
    }