Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 496
Vijay V. Avatar asked Dec 13 '25 21:12

Vijay V.


1 Answers

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);
}
like image 172
Filip Koblański Avatar answered Dec 15 '25 09:12

Filip Koblański



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!