【问题标题】:Laravel 4 - paginate ignore distinct in FluentLaravel 4 - 在 Fluent 中分页忽略不同
【发布时间】:2013-06-09 01:34:20
【问题描述】:

我提出流利的请求,分页清晰。我的问题是分页请求是在区分请求之前执行的

我的流利请求:

$candidates = DB::table('candidates')
            ->select('candidates.*')
            ->distinct()
            ->join('candidate_region', 'candidates.id', '=', 'candidate_region.candidate_id')
            ->join('candidate_job', 'candidates.id', '=', 'candidate_job.candidate_id')
            ->whereIn('candidate_region.region_id', $inputs['region'])
            ->whereIn('candidate_job.job_id', $inputs['job'])
            ->where('imavailable', '1')
            ->where('dateDisponible', '<=', $inputs['availableDate'])
            ->paginate(15);

我用 DB::getQueryLog() 测试我的请求

$query = DB::getQueryLog($candidates);

$query 显示制作编号页面的分页请求(带有计数):

5 => 
array (size=3)
     'query' => string 'select count(*) as aggregate from `candidates` inner join `candidate_region` on `candidates`.`id` = `candidate_region`.`candidate_id` inner join `candidate_job` on `candidates`.`id` = `candidate_job`.`candidate_id` where `candidate_region`.`region_id` in (?, ?, ?, ?, ?) and `candidate_job`.`job_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `imavailable` = ? and `dateDisponible` <= ?' (length=396)

而我的不同请求是在分页之后执行的:

6 => 
array (size=3)
  'query' => string 'select distinct `candidates`.* from `candidates` inner join `candidate_region` on `candidates`.`id` = `candidate_region`.`candidate_id` inner join `candidate_job` on `candidates`.`id` = `candidate_job`.`candidate_id` where `candidate_region`.`region_id` in (?, ?, ?, ?, ?) and `candidate_job`.`job_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) and `imavailable` = ? and `dateDisponible` <= ? limit 15 offset 15' (length=417)

分页前不同的执行请求怎么办?

谢谢

【问题讨论】:

    标签: laravel distinct laravel-4 fluent pagination


    【解决方案1】:
    <?php
         $presenter = new Illuminate\Pagination\BootstrapPresenter($candidates);
         $presenter->setLastPage($totalPages);
    ?>
    
    @if ($totalPages> 1)
         <ul class="pagination">
              {{ $presenter->render() }}
         </ul>
    @endif
    

    【讨论】:

      【解决方案2】:

      盲猜:试试

      DB::table('candidates')-&gt;...stuff...-&gt;get()-&gt;paginate(15)

      真正的答案:手动创建分页器:

      $candidates = DB::table('candidates')->...stuff...->get();
      
      $paginator = Paginator::make($candidates, count($candidates), 15);
      

      【讨论】:

      • -&gt;get-&gt;paginate 这是不可能的。分页器不接受对象且不返回对象
      • $paginator = Paginator::make($candidates, count($candidates), 15); 没关系,它返回了好的数字视图。但是,候选人都显示在页面上
      • 当然,因为手动分页器只生成页码,所以你必须自己过滤你的数组(简单)
      • 使用 beforeFilter 包含过滤器?
      • 更简单:$candidates = array_slice($candidates, $start, $count);php.net/manual/fr/function.array-slice.php
      猜你喜欢
      • 2012-09-19
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 1970-01-01
      • 2013-09-13
      • 2019-12-19
      • 2017-01-12
      • 1970-01-01
      相关资源
      最近更新 更多