Reputation: 759
I am trying to use pagination in Laravel 4 with quite a complex query (using Fluent query builder I think).
Query SQL Code
SELECT `description`, `email`, `website`, `telephone`, `purpose`, `services`,
`eligibilitycriteria`, `meetingplace`, `servicecosts`, `area`,
`servicetype`, `beneficiarytype`, `charitynumber`, `address`, `contacts`,
MATCH (
`name`,`description`,`email`,`website`,`telephone`,`purpose`,`services`,
`eligibilitycriteria`,`meetingplace`,`servicecosts`,`area`,`servicetype`,
`beneficiarytype`,`charitynumber`,`address`,`contacts`
)
AGAINST ("dan") AS Score0,
MATCH (`name`) AGAINST ("dan") AS Score1 FROM `directory_cache`
WHERE MATCH(
`description`,`email`,`website`,`telephone`,`purpose`,`services`,
`eligibilitycriteria`,`meetingplace`,`servicecosts`,`area`,`servicetype`,
`beneficiarytype`,`charitynumber`,`address`,`contacts`
)
AGAINST ("dan")
ORDER BY (Score0*1 + Score1*1.5) DESC
I am calling the paginate at the end of the query builder in my controller like follows:
$results = $query->paginate(20);
Then in my view I simply call $results->links
and I do get the pagination buttons for next page and the numbered pages etc.
But when I click on Page 2, this is where I encounter the problem.
My form uses POST to submit the search form. Both the get and post actions use the same view file to display the search form and results.
Please help and if you need any more information I am happy to edit the post and add it in, I am here all night!
Upvotes: 1
Views: 2145
Reputation: 11691
My suggestion would be to route both viewResults
and search
to the same route using Route::any()
so that you would need to perform seperate functionality for both actions
routes.php
Route::any('/list','Controller@viewResults');
Controller.php
function viewResults(){
$searchQuery = Input::get('searchQuery');
if(isset($searchQuery))
$query = DirectoryCache::where(...)->where(...);
else
$query = DirectoryCache::where(1, '=', 1);
$results = $query->paginate();
//This will append the searchQuery to your pagination links
$results->appends(array('searchQuery'=>$searchQuery));
}
So irrespective of you perform a POST
or GET
you would execute the same action and still be able to perform search and get the results. I hope this helps
Upvotes: 4
Reputation: 10153
You'll have to use GET or store the query in session, I don't see an easier way of doing this. Using POST like you do is bad practice anyway. You shoud POST and then redirect from there, either passing GET parameters or keeping the query in session.
function post_search() {
$query = DirectoryCache::where(...)->where(...);
Session::put('searchQuery', $query);
return Redirect::to_action('controller@viewResults');
}
function get_viewResults($page = 1) {
$perPage = 20;
$query = Session::get('searchQuery', DirectoryCache::where(1, '=', 1));
$query = $query->paginate();
$query->skip($page * $perPage)->take($perPage);
$results = $query->get();
...
}
I know it's Laravel 3, I haven't touched 4 yet, but the APIs should be almost the same.
Upvotes: 0