B L Praveen
B L Praveen

Reputation: 1990

How to work with pagination in Laravel Algolia search

Pagination is not working. It shows empty page on clicking 2 page.It is not fetching the second page from the search query. And also token is not showing on 2nd page url. Is this is the reason then how to add csrf_token to pagination
Route.

I am using algolia to search

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

Controller

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

View

@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

Upvotes: 0

Views: 666

Answers (2)

Ismoil  Shifoev
Ismoil Shifoev

Reputation: 6001

Try this one may be help

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

Update my answer sometimes help this

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

Upvotes: 1

Dhruv Raval
Dhruv Raval

Reputation: 1583

Try :

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

insted of

{{ $results->links() }}

Upvotes: 0

Related Questions