简体   繁体   English

搜索结果的分页 laravel 5.3

[英]Pagination for search results laravel 5.3

Pagination search results分页搜索结果

I have just started with Laravel and I am trying to make a search function with proper pagination.我刚刚开始使用 Laravel,我正在尝试使用适当的分页来制作搜索功能。 The function works for page one but on page two it doesn't.该功能适用​​于第一页,但在第二页上不起作用。 I think it's not giving the results to the next page but I can't seem to find an answer.我认为它没有将结果提供给下一页,但我似乎无法找到答案。


this is my search function inside IndexController:这是我在 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'));
}

this is my route:这是我的路线:

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

this is the URL of page two:这是第二页的网址:

/search?page=2

this is how I show my pagination:这就是我显示分页的方式:

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

the error:错误:

MethodNotAllowedHttpException in RouteCollection.php line 218:

Get error on request.根据要求获取错误。

Route:路线:

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

Error:错误:

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->Illuminate\Foundation\Http\{closure}(object(Request)) in Pipeline.php line 53
at Pipeline->Illuminate\Routing\{closure}(object(Request)) in CheckForMaintenanceMode.php line 46
at CheckForMaintenanceMode->handle(object(Request), object(Closure)) in Pipeline.php line 137
at Pipeline->Illuminate\Pipeline\{closure}(object(Request)) in Pipeline.php line 33
at Pipeline->Illuminate\Routing\{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

I hope my question is clear and in the right format.我希望我的问题很清楚并且格式正确。 Thank you in advance (sorry for my bad English)提前谢谢你(抱歉我的英语不好)


Answer:回答:

I ended up using the answer of this post in combination with some help of this post最后我用这篇文章的答案,结合一些帮助这个职位

I used a post function for the initial search and a get function for the following pages.我在最初的搜索中使用了 post 函数,在接下来的页面中使用了 get 函数。 This was possible because I'm now giving my search to the URL.这是可能的,因为我现在正在搜索 URL。


EDIT:编辑:

  • added the initial error.添加了初始错误。
  • added the Route::get error添加了Route::get错误
  • added answer添加答案

If you want to apply filters to the next page you should add them to your paginator like this:如果要将过滤器应用于下一页,则应将它们添加到分页器中,如下所示:

$product = Product::where('naam', 'LIKE', '%' . $q . '%')
        ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
        ->paginate(6);
$product->appends(['search' => $q]);

And change your route from post to get:并更改您的邮寄路线以获得:

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

快速查看方式(Lavarel 5.7)

$product->appends(Request::all())->links();
Route::get('product', function () {
    $product= App\product::paginate(15);

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

});

View:查看:

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

I assume you want to change pages with urls like this search/1 , search/2 ?我假设您想更改带有这样的网址的页面search/1 , search/2 First of all your route should be probably Route::post('search/{page?}') .首先,您的路线应该是Route::post('search/{page?}')

I'm not sure if only this change will work, but if it does not, you have to resolve page like this我不确定是否只有此更改有效,但如果无效,您必须像这样解析页面

public function search(\Illuminate\Http\Request $request, $page = 1)
{
    $q = $request->get('search');

    \Illuminate\Pagination\Paginator::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'));
}
$searchdata =  \Request::get( 'inputTextFieldname' ); \make as global
$searchresult = Modelname::where ( 'blogpost_title', 'LIKE', '%' .$searchdata . '%' )->paginate(2);
return view( 'search', compact('searchresult') );

and in your view page并在您的视图页面中

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

make your route to get method让你的路线得到方法

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

this will be done, thanks,这将完成,谢谢,

in my case, i've laravel 5.7 installed.就我而言,我已经安装了 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);

and my view files codes are我的视图文件代码是

for per_page select dropdown and search area对于per_page选择下拉列表和搜索区域

<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>

and my pagination generator code和我的分页生成器代码

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

If you are using search form with GET method then use something like these to preserve pagination withing search results.如果您使用带有 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());
    }

For pagination, you should create a simple form:对于分页,您应该创建一个简单的表单:

<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>

Pagination methods are here:分页方法在这里:

$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)

In your view file where you display pagination...在显示分页的视图文件中...

{{ $results->appends(Request::except('page'))->links() }}

appends keeps the query string value except "page". appends 保留查询字符串值,除了“page”。

在路由中使用 any 而不是 post Route::any('search', 'IndexController@search');

The easy way to solve this use your UsersController.php by adding ->withQueryString() to ->paginate(6)通过将 ->withQueryString() 添加到 ->paginate(6) 来解决这个问题的简单方法是使用您的 UsersController.php

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)->withQueryString();

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

and in the view file并在视图文件中

{{$product->links()}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM