Pagination for search results laravel 5.3

前端 未结 10 1371
无人及你
无人及你 2021-02-01 06:17

Pagination search results

I have just started with Laravel and I am trying to make a search function with proper pagination. The function works for page one but on pag

相关标签:
10条回答
  • 2021-02-01 06:54

    If you want to apply filters to the next page you should add them to your paginator like this:

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

    And change your route from post to get:

    Route::get('search', 'IndexController@search');
    
    0 讨论(0)
  • 2021-02-01 07:05

    in my case, i've laravel 5.7 installed.

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

    and my view files codes are

    for per_page select dropdown and search area

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

    and my pagination generator code

    {{ $items->appends(['search' => request()->search, 'per_page' => request()->per_page])->links() }}
    
    0 讨论(0)
  • 2021-02-01 07:07

    For pagination, you should create a simple form:

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

    Pagination methods are here:

    $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)
    
    0 讨论(0)
  • 2021-02-01 07:09

    I assume you want to change pages with urls like this search/1, search/2? First of all your route should be probably Route::post('search/{page?}').

    I'm not sure if only this change will work, but if it does not, you have to resolve page like this

    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'));
    }
    
    0 讨论(0)
提交回复
热议问题