2013-04-30 15 views
12

ho questo join:Utilizzando Distinti in laravel Fluent

Return DB::table('volunteer') 
      ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id') 
      ->select(array('*','volunteer.id AS link_id')) 
      ->where('is_published', '=', 1) 

ma restituisce sorprende record duplicati, quindi cerco di usare distinct():

Return DB::table('volunteer') 
      ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id') 
      ->select(array('*','volunteer.id AS link_id')) 
         ->distinct() 
      ->where('is_published', '=', 1) 

ma voglio utilizzare distinct()su un specifico campo singolo che sarei facilmente in grado di fare in SQL. Sembra che distinct() non prenda parametri, cioè non posso dire distinct('volunteer.id').

Qualcuno può indicarmi come posso rimuovere i miei record duplicati? Scommetto che questa è un'altra schiava sulla fronte per me.

+0

Ovviamente sono completamente stupido qui e devo aggiungere '-> group_by ('volunteer.id')' così come '-> distinct()'. –

+0

Ma includendo '-> group_by ('volunteer.id')' fa scomparire i miei collegamenti di paginazione !! ??? –

+0

Credo che ci sia stato un bug con paginazione e group_by per un po 'ora in Laravel 3; Non so se sia stato risolto in Laravel 4. –

risposta

26

Nel mio progetto ho cercato distinct() e groupby() troppo ed entrambi lavoravano:

//Distinct version. 
Company_Customer_Product::where('Company_id', '=', $companyid)->distinct()->get(array('Customer_id')); 
//Goup by version. 
Company_Customer_Product::where('Company_id', '=', $companyid)->groupby('Customer_id')->get(array('Customer_id')); 

In base a questo, distinct() dovrebbe funzionare nel vostro caso, basta usare con get():

Return DB::table('volunteer') 
    ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id') 
    ->select(array('*','volunteer.id AS link_id')) 
    ->distinct() 
    ->where('is_published', '=', 1) 
    ->get(array('volunteer.id')); 

Altrimenti non è necessario distinct() quando si utilizza groupby() quindi è possibile utilizzare semplicemente:

Return DB::table('volunteer') 
    ->join('volunteer_volunteer_category', 'volunteer_volunteer_category.volunteer_id', '=', 'volunteer.id') 
    ->select(array('*','volunteer.id AS link_id')) 
    ->group_by('volunteer.id') 
    ->where('is_published', '=', 1) 
    ->get(array('volunteer.id')); 
Problemi correlati