Laravel分页不适用于日期搜索

时间:2015-12-30 23:23:14

标签: php laravel pagination

我的应用程序中有几个搜索表单,其中分页效果很好,但是当涉及到日期搜索时,我只获得第一页,当我尝试转到第二页时,我得到一个未定义的$ ticket变量。当我查看网址时,我可以看到它没有将日期值带到下一页。

这是我的代码:

<tbody class="searchTable">
  @foreach($ticket as $tickets)
    <tr>
        <td>
            {{Carbon::parse($tickets->created_at)->format('m/d/Y')}}
        </td>
        <td><a href="/ticket/{{$tickets->id}}">{{$tickets->id}}</a></td>
        <td><a href="/ticket/client/{{$tickets->clientName}}">{{$tickets->clientName}}</a></td>
        <td>
            {{Carbon::parse($tickets->dueDate)->format('m/d/Y')}}   
        </td>
        <td>{{$tickets->refNumber}}</td>
        <td>{{$tickets->invoiceNumber}}</td>
        <td>{{$tickets->jobLocation}}</td>
        <td>{{$tickets->workDescription}}</td>
        <td>{{$tickets->jobStatus}}</td>
    </tr>
  @endforeach
</tbody> 
{!! $ticket->appends(Request::only('_token','search'))->render() !!}

这是控制器:

  $ticket = DB::table('tickets')
            ->whereBetween('created_at', [$newDateFrom, $newDateTo])
            ->orderBy('created_at', 'desc')
            ->paginate(10);
  return view('ticketsView', compact('ticket'));

1 个答案:

答案 0 :(得分:1)

这是我的解决方案:

我对不同的搜索请求有相同的视图,因此我将结果附加到分页器,以便它也可以在下一页上显示。名称是搜索表单中name属性的值。在我的情况下,我有多个。这是一个例子:

{!! $ticket->appends(Request::only(['dateFrom'=>'dateFrom', 'dateTo'=>'dateTo', 'search'=>'search', 'filter'=>'filter', 'dueDateFrom'=>'dueDateFrom','dueDateTo'=>'dueDateTo']))->render() !!} 

现在,如果我的搜索结果将包含在URL dateTo和dateFrom值中,那么它将保存到所有页面。了解这些值来自搜索表单的name属性非常重要。

以下是一个例子:

 <form class="form-horizontal" role="form" method="GET" action="/ticket/searchresults" accept-charset="UTF-8" enctype="multipart/form-data">
   <div class="text-centered">
     <p><strong>Search by dates:</strong></p>
   </div>
   <div class="form-group">
     <label for="filter">Select Client (optional)</label>
     <select class="form-control" name="filter" type="text">
       <option disabled selected> -- select client -- </option>
       @foreach($selectClient as $selectClients)
         <option value="{{$selectClients->name}}">{{$selectClients->name}}</option>
       @endforeach
     </select>
   </div>
   <div class="form-group">
     <label for="dateFrom">From:</label>
     <input class="datepicker2 form-control" name="dateFrom" type="text" required/>
   </div>
   <div class="form-group">
     <label for="dateTo">To:</label>
     <input class="datepicker2 form-control" name="dateTo" type="text" required/>
   </div>
   <button type="submit" class="btn btn-primary"><span class="fa fa-fw fa-search"></span></button>
 </form>