繁体   English   中英

搜索结果的分页 laravel 5.3

[英]Pagination for search results laravel 5.3

分页搜索结果

我刚刚开始使用 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

我希望我的问题很清楚并且格式正确。 提前谢谢你(抱歉我的英语不好)


回答:

最后我用这篇文章的答案,结合一些帮助这个职位

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


编辑:

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

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

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

并更改您的邮寄路线以获得:

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

});

查看:

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

我假设您想更改带有这样的网址的页面search/1 , search/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'));
}
$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']);

这将完成,谢谢,

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

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

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

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

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

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

appends 保留查询字符串值,除了“page”。

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

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

并在视图文件中

{{$product->links()}}

暂无
暂无

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

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