【问题标题】:Pagination for search results laravel 5.3搜索结果分页 laravel 5.3
【发布时间】:2017-01-12 13:34:18
【问题描述】:

分页搜索结果

我刚开始使用 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::post('search{page?}', 'IndexController@search');

这是第二页的网址:

/search?page=2

这就是我显示分页的方式:

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

错误:

MethodNotAllowedHttpException in RouteCollection.php line 218:

根据请求获取错误。

路线:

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

我希望我的问题是明确的并且格式正确。提前谢谢你(对不起我的英语不好)


答案:

我最终结合this帖子的一些帮助使用了这篇文章的答案

我在初始搜索中使用了 post 函数,在接下来的页面中使用了 get 函数。这是可能的,因为我现在正在对 URL 进行搜索。


编辑:

  • 添加了初始错误。
  • 添加了Route::get 错误
  • 添加了答案

【问题讨论】:

  • 你能发布完整的错误堆栈跟踪吗?
  • 我在想。我可以使用 POST 路由进行初始搜索并使用第二个 GET 路由进行下一页修复吗?我认为我必须将 ?page=# 缓存为一条路线。

标签: php laravel search laravel-5 pagination


【解决方案1】:

如果您想将过滤器应用到下一页,您应该像这样将它们添加到您的分页器中:

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

并将您的路线从 post 更改为 get:

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

【讨论】:

  • 看来我的大脑直到中午才 100% 工作。这确实是正确答案。对这个问题。还有,不应该是Route::get('search/{page?}')吗?
  • @Skysplit 坦率地说,{page?} 应该从 url 中完全删除。
  • 是的,你是对的。如果添加了页面解析器会很有用。
  • 这适用于转到第 2 页,但要发布我的搜索,我需要一个帖子。有什么解决方法吗?
  • @casperspruit 我不确定你为什么需要POST 搜索。通常,当您想更改应用程序状态/数据时,您使用POST。搜索不会改变任何东西,因此您可以使用GET。如果你真的想使用POST,那么你必须覆盖分页器视图并在其中使用post方法。
【解决方案2】:

快速查看视图 (Lavarel 5.7)

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

【讨论】:

    【解决方案3】:
    Route::get('product', function () {
        $product= App\product::paginate(15);
    
        $product->setPath('custom/url');
    
    });
    

    查看:

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

    【讨论】:

      【解决方案4】:

      我假设您想更改带有 search/1search/2 之类的网址的页面?首先你的路线应该是Route::post('search/{page?}')

      我不确定是否只有此更改会起作用,但如果不起作用,您必须像这样解析页面

      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'));
      }
      

      【讨论】:

      • 编辑:我的 url 就像 search?page=2,分页默认是这样的,所以我应该如何改变它。您的函数给出了与我的相同错误“RouteCollection.php 第 218 行中的 MethodNotAllowedHttpException:”
      【解决方案5】:
      $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::get('/search', ['as' => 'search', 'uses' => 'searchController@index']);
      

      这将完成,谢谢,

      【讨论】:

        【解决方案6】:

        就我而言,我已经安装了 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);
        

        我的视图文件代码是

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

        和我的分页生成器代码

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

        【讨论】:

          【解决方案7】:

          对于分页,你应该创建一个简单的表单:

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

          【讨论】:

          • 谢谢,但我认为这不是最干净的方法,尽管这些信息现在对我有帮助。
          【解决方案8】:

          如果您使用带有 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());
              }
          

          【讨论】:

            【解决方案9】:

            解决此问题的简单方法是使用您的UsersController.php,将-&gt;withQueryString() 添加到-&gt;paginate(6)

            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'));
            }
            

            在视图文件中

            {{$product->links()}}
            

            【讨论】:

            • 请编辑您的问题以解释 withQueryString() 在这种情况下的作用。
            • 嗨 miken,函数 withQueryString() 将在 laravel 7 和 8 上运行。如您在官方文档laravel.com/docs/8.x/pagination#appending-query-string-values 中所见,它将发出 URL 请求 ?page=2
            • 正如我所说,edit 你的问题来解释这一点。
            【解决方案10】:

            在显示分页的视图文件中...

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

            appends 保留除“page”之外的查询字符串值。

            【讨论】:

            • 在 laravel 8 中运行良好
            【解决方案11】:

            在路由中使用 any 代替 post Route::any('search', 'IndexController@search');

            【讨论】:

              猜你喜欢
              • 2017-02-24
              • 1970-01-01
              • 2021-03-24
              • 2018-01-17
              • 1970-01-01
              • 2012-07-31
              • 2018-11-19
              • 2019-10-03
              相关资源
              最近更新 更多