Laravel:搜索结果搜索?page = 2什么都不显示,只是空白页面

时间:2017-11-11 05:06:55

标签: php laravel search pagination

我的分页工作正常,但使用搜索组件时,它只显示结果的第一页。 没有搜索的我的网页网址如下所示: http://localhost:8000/dictionary-management/postcode?page=2 它的工作正常。

我的搜索第一页网址: http://localhost:8000/dictionary-management/postcode/search 它的工作正常。

我的搜索第二页网址: http://localhost:8000/dictionary-management/postcode/search?page=2 并没有什么可显示的,只有空白页。

这是我的控制器搜索方法:

public function search(Request $request) {
    $constraints = [
        'postcode' => $request['postcode'],
        'address' => $request['address']
    ];

    $postcodes = $this->doSearchingQuery($constraints);
    return view('dictionary-mgmt/postcode/index', ['postcodes' => $postcodes, 'searchingVals' => $constraints]);
}

private function doSearchingQuery($constraints) {
    $query = Postcode::query();
    $fields = array_keys($constraints);
    $index = 0;
    foreach ($constraints as $constraint) {
        if ($constraint != null) {
            $query = $query->where( $fields[$index], 'like', '%'.$constraint.'%');
        }

        $index++;
    }
    return $query->Paginate(5);
}

这是我的路线:

Route::resource('dictionary-management/postcode', 'PostCodeController');
Route::post('dictionary-management/postcode/search', PosstCodeController@search')->name('postcode.search');

这是我的索引视图:

@extends('dictionary-mgmt.postcode.base')
@section('action-content')
    <!-- Main content -->
    <section class="content">
      <div class="box">
  <div class="box-header">
    <div class="row">
        <div class="col-sm-8">
          <h3 class="box-title">List kodów pocztowych</h3>
        </div>
        <div class="col-sm-4">
          <a class="btn btn-primary pull-right" href="{{ route('postcode.create') }}">Dodaj nowy kod pocztowy</a>
        </div>
    </div>
  </div>
  <!-- /.box-header -->
  <div class="box-body">
      <div class="row">
        <div class="col-sm-6"></div>
        <div class="col-sm-6"></div>
      </div>
      <form method="POST" action="{{ route('postcode.search') }}">
         {{ csrf_field() }}
          @component('layouts.search', ['title' => 'Szukaj'])
              @component('layouts.two-cols-search-row', ['items' => ['postcode', 'address'], 'title' => ['Kod','Adres'],
              'oldVals' => [isset($searchingVals) ? $searchingVals['postcode'] : '', isset($searchingVals) ? $searchingVals['address'] : '']])
              @endcomponent
          @endcomponent
      </form>
    <div id="example2_wrapper" class="dataTables_wrapper form-inline dt-bootstrap">
      <div class="row">
        <div class="col-sm-12">
          <table id="example2" class="table table-bordered table-hover dataTable" role="grid" aria-describedby="example2_info">
            <thead>
              <tr role="row">
                <th width="5%">Kod pocztowy</th>
                <th width="40%">Adres</th>
                <th width="10%">Miejscowość</th>
                <th width="10%">Województwo</th>
                <th width="10%">Powiat</th>
                <th>Akcja</th>
              </tr>
            </thead>
            <tbody>
            @foreach ($postcodes as $postcode)
                <tr role="row" class="odd">
                  <td>{{ $postcode->postcode }}</td>
                  <td>{{ $postcode->address }}</td>
                  <td>{{ $postcode->city }}</td>
                  <td>{{ $postcode->voivodeship }}</td>
                  <td>{{ $postcode->county }}</td>
                    <td>
                    <form class="row" method="POST" action="{{ route('postcode.destroy', ['id' => $postcode->id]) }}" onsubmit = "return confirm('Czy napewno usunąć?')">
                        <input type="hidden" name="_method" value="DELETE">
                        <input type="hidden" name="_token" value="{{ csrf_token() }}">
                        <a href="{{ route('postcode.edit', ['id' => $postcode->id]) }}" class="btn btn-warning col-sm-3 col-xs-5 btn-margin">
                        Edytuj
                        </a>
                        <button type="submit" class="btn btn-danger col-sm-3 col-xs-5 btn-margin">
                          Usuń
                        </button>
                    </form>
                  </td>
              </tr>
            @endforeach
            </tbody>
            <tfoot>
              <tr>
                  <th width="5%">Kod pocztowy</th>
                  <th width="40%">Adres</th>
                  <th width="10%">Miejscowość</th>
                  <th width="10%">Województwo</th>
                  <th width="10%">Powiat</th>
                  <th>Akcja</th>
              </tr>
            </tfoot>
          </table>
        </div>
      </div>
      <div class="row">
        <div class="col-sm-5">

        </div>
        <div class="col-sm-7">
          <div class="dataTables_paginate paging_simple_numbers" id="example2_paginate">
            {{ $postcodes->links() }}
          </div>
        </div>
      </div>
    </div>
  </div>
  <!-- /.box-body -->
</div>
    </section>
    <!-- /.content -->
  </div>
@endsection

这是我的搜索组件:

   <div class="row">
  @php
    $index = 0;
  @endphp
  @foreach ($items as $item)
    <div class="col-md-6">
      <div class="form-group">
          @php
            $stringFormat =  strtolower(str_replace(' ', '', $item));
          @endphp
          <label for="input<?=$stringFormat?>" class="col-sm-3 control-label">{{$title[$index]}}</label>
          <div class="col-sm-9">
            <input value="{{isset($oldVals) ? $oldVals[$index] : ''}}" type="text" class="form-control" name="<?=$stringFormat?>" id="input<?=$stringFormat?>" placeholder="{{$title[$index]}}">
          </div>
      </div>
    </div>
  @php
    $index++;
  @endphp
  @endforeach
</div>

请帮助,我不知道我的错误在哪里......

1 个答案:

答案 0 :(得分:1)

试试这个

 {{ $postcodes->appends(request()->input())->links()}}

而不是{{ $postcodes->links() }}