1
votes

Got a question if anyone can help out.

I have a query that has a collection of parameters and displays some results.

DB::table()
  ->join()
  ->where()
  ->orderby()
  ->select()
  ->get();

But the wheres are generated by a form input in the view. Basically this is a bunch of filters to get a table of results. I want to paginate that. if I change the get() to paginate(), and call $result->links() in the template, it does indeeed paginate and generates a bunch of results for me, however the problem is that when you move away from page 1, the links are just a _GET parameter and all the filter input does not get applied.

Is there a way I can have the pagination AND filters going at the same time? What is the laravel way of handling that? Or would I have to build some way of handling filters and pages? Any tips on that?

Cheers!

* Solution * The solution was to make the form use GET method and persist the old filters to the Pagination using ->appends() method. Below is the modified code to do that if anyone else is looking.

$results = $query->paginate($this->report->inputs->per_page);
$query = array_except( Input::query(), Paginator::getPageName() );
$results->appends($query);
1

1 Answers

0
votes

Yes, here's how I do it:

$orderBy = Input::get('orderBy', 'created_at');
$order   = Input::get('order', 'DESC');
$limit   = Input::get('limit', 100);
$name    = Input::get('name', '');

$result = DB::table()
    ->where('name', 'LIKE', '%' . $name . '%')
    ->orderBy($orderBy, $order)
    ->paginate($limit);