分页/搜索Laravel问题 [英] Paginate / Search Laravel issue
问题描述
经过几次交谈后,我终于到达了某个地方,但是在分页现在可以进行搜索了吗?page = 2之后,我得到了登录用户的json数组,而不是结果的第2页. /p>
这是我的控制器:
public function search()
{
$q = Input::get('term');
if($q && $q != ''){
$searchTerms = explode(' ', $q);
$query = DB::table('wc_program');
if(!empty($searchTerms)){
foreach($searchTerms as $term) {
$query->where('JobRef', 'LIKE', '%'. $term .'%');
$query->orwhere('Road', 'LIKE', '%'. $term .'%');
}
}
$results = $query->paginate(10);
return View::make('layouts.results', compact('results'));
}
}
路线: 路线:: get('/search','HomeController @ search');
那么我该如何解决呢?
您需要将查询字符串附加到分页中.
在您的控制器中,将查询字符串传递给视图.
return View::make('layouts.results', compact('results', 'q'));
您认为(results.blade.php):
将查询字符串追加到分页上,否则在第2页中将不会获得任何结果.
<?php echo $results->appends(array('term' => $q))->links(); ?>
After a couple of conversations on here i'm finally getting somewhere but after the pagintion now works when i got to search?page=2 i get a json array of the logged in user and not page 2 of the results.
Here's my controller:
public function search()
{
$q = Input::get('term');
if($q && $q != ''){
$searchTerms = explode(' ', $q);
$query = DB::table('wc_program');
if(!empty($searchTerms)){
foreach($searchTerms as $term) {
$query->where('JobRef', 'LIKE', '%'. $term .'%');
$query->orwhere('Road', 'LIKE', '%'. $term .'%');
}
}
$results = $query->paginate(10);
return View::make('layouts.results', compact('results'));
}
}
Route: Route::get('/search', 'HomeController@search');
So how can i work around this?
You need to append query string to the pagination.
in your controller, pass the query string to the view.
return View::make('layouts.results', compact('results', 'q'));
In your view (results.blade.php):
append the query string to the pagination otherwise in page 2 you will not get any result.
<?php echo $results->appends(array('term' => $q))->links(); ?>
这篇关于分页/搜索Laravel问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!