Laravel 4 - paginate ignore distinct in Fluent

人盡茶涼 提交于 2019-12-02 04:26:59

问题


I make fluent request with distinct and paginate. My problem is that the paginate request is execute before disinct request

My Fluent request :

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

I test my request with DB::getQueryLog()

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

$query show that paginate request (with count) for make number page :

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)

And my request with distinct is execute after paginate :

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)

How to do for execute request distinct before the paginate ?

Thanks


回答1:


Blind guess : try

DB::table('candidates')->...stuff...->get()->paginate(15)

Real answer: Create your paginator manually:

$candidates = DB::table('candidates')->...stuff...->get();

$paginator = Paginator::make($candidates, count($candidates), 15);



回答2:


<?php
     $presenter = new Illuminate\Pagination\BootstrapPresenter($candidates);
     $presenter->setLastPage($totalPages);
?>

@if ($totalPages> 1)
     <ul class="pagination">
          {{ $presenter->render() }}
     </ul>
@endif


来源:https://stackoverflow.com/questions/17073971/laravel-4-paginate-ignore-distinct-in-fluent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!