搜索结果的分页laravel 5.3

时间:2016-09-05 08:04:20

标签: php laravel search laravel-5 pagination

分页搜索结果

我刚刚开始使用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错误
  • 添加了回答

10 个答案:

答案 0 :(得分:24)

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

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

并改变你的路线从发布到获取:

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

答案 1 :(得分:3)

Route::get('product', function () {
    $product= App\product::paginate(15);

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

});

查看:

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

答案 2 :(得分:2)

我假设您要更改包含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'));
}

答案 3 :(得分:2)

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

这将完成,谢谢,

答案 4 :(得分:1)

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

答案 5 :(得分: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());
    }

答案 6 :(得分:1)

快速查看(Lavarel 5.7)

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

答案 7 :(得分:0)

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

<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 :(得分:0)

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

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

追加会保留查询字符串值(“页面”除外)。

答案 9 :(得分:0)

在路线中使用任何内容,而不是发布 路线:: any('搜索','IndexController @ search');