如何在Laravel Algolia搜索中使用分页功能

时间:2018-12-20 08:37:42

标签: laravel algolia

分页不起作用。单击2页时显示空白页。它不是从搜索查询中获取第二页。而且令牌没有显示 第二页网址。这就是为什么要在分页中添加csrf_token的原因
路线。

我正在使用阿尔戈利亚搜索

    Route::get('posts/search','PostController@search')->name('posts.search');

控制器

    public function search(Request $request){

    if($request->has('q')) {
         $request->flashOnly('q');
         $results = Post::search($request->q)->paginate(5);
    } else {
        $results = [];
    }
    return view('posts.search')->with('results', $results);
}

查看

@extends('layouts.app')

@section('content')
<div class="container">
<h1>Search for Posts</h1>
<form action="{{ route('posts.search') }}" method="get">
  {{ csrf_field() }}

  <div class="input-group">
    <input type="text" name="q" class="form-control input-lg" placeholder="Search for a post..." value="{{ old('q') }}"/>
    <span class="input-group-btn">
      <button class="btn btn-default btn-lg" type="submit">Search</button>
    </span>
  </div>
</form>
<hr />

@foreach ($results as $post)
  <div class="row" style="margin-top: 20px;">
    <div class="col-md-8">
      <a href="{{ route('posts.show', $post->id) }}"><h3>{{ $post->title }}</h3></a>
    </div>
    <div class="col-md-4">
      @if ($post->published)
        <h4><span class="label label-success pull-right">PUBLISHED</span><h4>
      @else
        <h4><span class="label label-default pull-right">DRAFT</span><h4>
      @endif
    </div>
  </div>
  <div class="row">
    <div class="col-md-12">
      <p>
        {{ str_limit($post->content, 250) }}
      </p>
    </div>
  </div>
@endforeach

@if (count($results) > 0)
  <hr />

  <div class="text-center">
    {{ $results->links() }}
  </div>
@endif

@endsection

2 个答案:

答案 0 :(得分:0)

尝试:

{{$results->appends(request()->query())->links()}}

装有

{{ $results->links() }}

答案 1 :(得分:0)

尝试这个可能会有所帮助

 // Making sure the user entered a keyword.
       if($request->has('q')) {
                // Using the Laravel Scout syntax to search the posts table.
                $results = Post::search($request->get('q'))->paginate(15);
            }

更新我的答案有时可以帮助

 return view('posts.search',['results' => $results]);