Php 搜索结果分页laravel 5.3 分页搜索结果

Php 搜索结果分页laravel 5.3 分页搜索结果,php,laravel,search,laravel-5,pagination,Php,Laravel,Search,Laravel 5,Pagination,我刚刚开始使用Laravel,我正在尝试使用适当的分页来实现搜索功能。该函数适用于第一页,但在第二页不适用。我想这不会给下一页结果,但我似乎找不到答案 这是我在IndexController中的搜索功能: 这是我的路线: 这是第二页的URL: 以下是我显示分页的方式: 错误: 请求时获取错误 路线: Route::get('search/{page?}', 'IndexController@search'); 错误: MethodNotAllowedHttpException in Route

我刚刚开始使用Laravel,我正在尝试使用适当的分页来实现搜索功能。该函数适用于第一页,但在第二页不适用。我想这不会给下一页结果,但我似乎找不到答案

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

这是我的路线:

这是第二页的URL:

以下是我显示分页的方式:

错误:

请求时获取错误

路线:

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错误 补充答案
我想你想改变网页的URL,比如搜索/1,搜索/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'));
}

如果要将筛选器应用到下一页,应将其添加到分页器中,如下所示:

$product = Product::where('naam', 'LIKE', '%' . $q . '%')
        ->orWhere('beschrijving', 'LIKE', '%' . $q . '%')
        ->paginate(6);
$product->appends(['search' => $q]);
并将您的路线从post更改为:

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


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

<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>
在您的查看页面中

{{$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);
我的视图文件代码是

对于每页,选择下拉菜单和搜索区域


如果您将搜索表单与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());
    }

快速查看Lavarel 5.7

$product->appendsRequest::all->links;
在显示分页的视图文件中

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

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

使用路由中的任何值,而不是post
路线::任何‘搜索’,'IndexController@search';

你能发布完整的错误堆栈跟踪吗?我在想。我可以用一个初始搜索的POST路径和下一页的第二个GET路径来修复它吗?我想,我得把cach?page=当作一条路线。看来我的大脑要到中午才能100%工作。这确实是正确的答案。对这个问题。另外,它不应该是Route::get'search/{page?}?@skyspilt坦率地说,{page?}应该从url中完全删除。是的,你是对的。这将是有用的,如果有一个页面解析器添加。这是去第2页的工作,但要发布我的搜索我需要一个职位。有什么解决办法吗?@casperspruit我不知道你为什么需要发布搜索。当您想要更改应用程序状态/数据时,通常使用POST。搜索不会改变任何东西,因此可以使用GET。如果你真的想使用POST,那么你必须覆盖paginator视图,并在其中使用POST方法。编辑:我的URL类似于搜索?page=2,默认情况下分页是这样做的,所以我应该如何更改它。您的函数给出的错误与我的方法相同RouteCollection.php第218行中NotAllowedHttpException:谢谢,但我认为这不是最干净的方法,尽管这些信息现在对我有所帮助。
{{ $product->appends(['search' => Request::get('page')])->links() }}
<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)
$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']);
$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() }}
 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());
    }
{{ $results->appends(Request::except('page'))->links() }}