others - PHP - Laravel 5.3搜索的结果分页

59 2

分页搜索结果

我刚开始使用Laravel,我正在尝试创建一个搜索函数使用适当的分页,该函数在第一页工作,但是在第二页,它就不工作了。

这是我在IndexController中的搜索功能:


public function search()


{


 $q = Input::get('search');



 # going to next page is not working yet


 $product = Product::where('naam', 'LIKE', '%' . $q . '%')


 ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')


 ->paginate(6);



 return view('pages.index', compact('product'));


}



这是我的route:


Route::post('search{page?}', 'IndexController@search');



这是第二页的URL :

 
/search?page=2



 

下面是我展示分页的方式:


{{ $product->appends(Request::get('page'))->links()}}



错误:


MethodNotAllowedHttpException in RouteCollection.php line 218:



请求时出错。

Route :


Route::get('search/{page?}', 'IndexController@search');



错误:


MethodNotAllowedHttpException in RouteCollection.php line 218:


in RouteCollection.php line 218


at RouteCollection->methodNotAllowed(array('GET', 'HEAD')) in RouteCollection.php line 205


at RouteCollection->getRouteForMethods(object(Request), array('GET', 'HEAD')) in RouteCollection.php line 158


at RouteCollection->match(object(Request)) in Router.php line 780


at Router->findRoute(object(Request)) in Router.php line 610


at Router->dispatchToRoute(object(Request)) in Router.php line 596


at Router->dispatch(object(Request)) in Kernel.php line 267


at Kernel->IlluminateFoundationHttp{closure}(object(Request)) in Pipeline.php line 53


at Pipeline->IlluminateRouting{closure}(object(Request)) in CheckForMaintenanceMode.php line 46


at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137


at Pipeline->IlluminatePipeline{closure}(object(Request)) in Pipeline.php line 33


at Pipeline->IlluminateRouting{closure}(object(Request)) in Pipeline.php line 104


at Pipeline->then(object(Closure)) in Kernel.php line 149


at Kernel->sendRequestThroughRouter(object(Request)) in Kernel.php line 116


at Kernel->handle(object(Request)) in index.php line 53



我为初始搜索使用了post函数,对之后的页面使用了get函数,

时间: 原作者:

57 5

如果要将过滤器应用到下一页,应将它添加到分页程序中,如下所示:


$product = Product::where('naam', 'LIKE', '%' . $q . '%')


 ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')


 ->paginate(6);


$product->appends(['search' => $q]);



并改变你从post到get的route:


Route::get('search', 'IndexController@search');



原作者:
119 2

Route::get('product', function () {


 $product= Appproduct::paginate(15);



 $product->setPath('custom/url');



});



视图:


{{ $product->appends(['search' => Request::get('page')])->links() }}



原作者:
132 1

假设你希望更改带有此search/1的url的页面,search/2? 首先,你的路由应该是Route::post('search/{page?}')

我不确定是否只有这种变化,但是如果不是,你必须解决这个页面,


public function search(IlluminateHttpRequest $request, $page = 1)


{


 $q = $request->get('search');



 IlluminatePaginationPaginator::currentPageResolver(function () use ($page) {


 return $page;


 });



 # going to next page is not working yet


 $product = Product::where('naam', 'LIKE', '%' . $q . '%')


 ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')


 ->paginate(6);



 return view('pages.index', compact('product'));


}



原作者:
140 0

$searchdata = Request::get( 'inputTextFieldname' ); make as global


$searchresult = Modelname::where ( 'blogpost_title', 'LIKE', '%' .$searchdata . '%' )->paginate(2);


return view( 'search', compact('searchresult') );



在你的视图页面中


{{$searchresult->appends(Request::only('inputTextFieldname'))->links()}}



让你的route得到方法


Route::get('/search', ['as' => 'search', 'uses' => 'searchController@index']);



这将完成,谢谢,

原作者:
140 5

对于分页,应创建一个简单窗体:


<form action="{{URL::to('/search')}}" method="post">


 <input type="hidden" name="query"/>


 <select name="pages">


 @for($p = 1; $p < $products->lastPage(); $p++ )


 <option value="{{ $p }}">{{ $p }}</option>


 @endfor


 </select>


</form>



这里有分页方法:


$results->count()


$results->currentPage()


$results->firstItem()


$results->hasMorePages()


$results->lastItem()


$results->lastPage() (Not available when using simplePaginate)


$results->nextPageUrl()


$results->perPage()


$results->previousPageUrl()


$results->total() (Not available when using simplePaginate)


$results->url($page)



原作者:
101 0

在我的例子中,安装了Laravel 5.7.


$perPage = $request->per_page ?? 10;



$data['items'] = User::where('name', 'like', '%'. $request->search . '%')


 ->paginate($perPage)


 ->appends(['search' => $request->search, 'per_page' => $request->per_page]);



 return view('users.index', $data);



我的档案里写着

对于每页的选择下拉列表和搜索区域


<form role="form" class="form-inline" method="get" action='{{ url('/user') }}'>


 <div class="row">


 <div class="col-sm-6">


 <div class="dataTables_length">


 <label>Show


 <select name="per_page"


 onchange="this.form.submit()"


 class="form-control input-sm">


 <option value=""></option>


 <option value="10" {{ $items->perPage() == 10 ? 'selected' : '' }}>10


 </option>


 <option value="25" {{ $items->perPage() == 25 ? 'selected' : '' }}>25


 </option>


 <option value="50" {{ $items->perPage() == 50 ? 'selected' : '' }}>50


 </option>


 </select>


 entries


 </label>


 </div>


 </div>



<div class="col-sm-6">


 <div class="dataTables_filter pull-right">


 <div class="form-group">


 <label>Search: &nbsp;


 <input type="search" name="search" class="form-control input-sm"


 placeholder="Name" value="{{ request()->search }}">


 </label>


 </div>


 </div>


</div>



我的分页生成器代码


{{ $items->appends(['search' => request()->search, 'per_page' => request()->per_page])->links() }}



原作者:
106 1

如果你使用的是带有get方法的搜索表单,那么使用类似这样的方法来保存搜索结果的分页。


 public function filter(Request $request)


 { 


 $filter = $request->only('name_operator','name_value','email_operator','email_value', 'phone_operator','phone_value', 'gender_value', 'age_operator','age_value');


 $contacts = $this->repo->getFilteredList(array_filter($filter));


 $contacts->appends($filter)->links(); //Continue pagination with results


 return view('dashboard::index', compact('contacts'))->withInput($request->all());


 }



原作者:
146 1

一个快速的方式(Lavarel 5.7)


$product->appends(Request::all())->links();



原作者:
...