2
votes

I have a query returning hundreds of database records.

As I am trying to paginate it using Laravel 4.2 method. Links are showing and they direct me to search-view?page=1 and so on, but the links open empty pages with no results. Only the first page contains results.

When I am trying to show these results without pagination the page never ends so I need to have it paginated.

Controller:

$results = $this->model->where($searchByColumn, '=', $searchKey)
                       ->paginate(30);

return View::make('search-view')
            ->with('results', $results);

View:

@foreach ($results as $result)
   {{$result}} <br>
@endforeach

{{$results->links()}}

Any help on this please?

2
provide the code for route search-view and its controllerMahdi Younesi
@MahdiYounesi Route only runs the code from the first two blocks of code I provided here. There is nothing much else in there. I am getting results from database and passing them to the view, thats all.divHelper11
try this paginate($30, ['*'], 'page', $request->page);Sohel0415
Have you tried APP_DEBUG=true? No errors in the log?brombeer
The issue is still open. Anyone :D?divHelper11

2 Answers

1
votes

I guess you don't see any record on the second page probably because $searchByColumn and $searchKey are not defined, that is are defined only on the first page because entered as input of a form but not on the other pages if you follow the basic paginator links.

To persist the value of $searchByColumn and $searchKey through consecutive requests you can append them to the links generated by the paginator, something like that:

{{ $results->appends(
    array('searchByColumn' => $searchByColumn, 
    'searchKey ' => $searchKey ))->links() }}

Adjust the code to follow the names of your parameters and don't forget to pass them to the view.

0
votes

Try something just like this compact help you to solved your problem

  return View::make('search-view', compact('results'))
                ->with('results', $results);

When doing compact you're already including the $results var to be used as $results in the view, exactly the same you'd do using with('results', $results). So this last can be removed. If you want to load more vars using compact just separe the variable names by commas.