Consultation in a relationship Many to Many in Laravel 4

5

Well, I'm "stuck" on a problem with a client report.

I have the following relationship:

  • Module (id, name)
  • Associate (id, name, upper_id)
  • Associates_Modules (modulo_id, associated_id, class, enrolled, attending, dropping)

What I need to do is: list all modules relative to a "superior_id" , with the following information:

  • How many classes belong to this module = Here is the first problem: Class is a varchar .. just a description, for example: Class 45 from 08:00 a.m. to 9:00 p.m.
  • how many associated with this module are present
  • how many associates pertaining to this module are enrolled
  • how many associated with this module have given up

In short: I do not know the best way to do this relationship (many to many?), and what better way to do this query, mainly because the class is a varchar and not a any module (which I could do a count through the default relationship).

Any information you need let me know.

    
asked by anonymous 17.08.2014 / 00:38

1 answer

2

It would look something like this: This is the best way to use it, ie Query Builder since Eloquent gets half-plastered.

DB::table('associados_modulos')            
        ->join('modulo', 'associados_modulos.modulo_id', '=', 'modulo.id')
        ->join('associado', 'associados_modulos.associado_id', '=', 'associado.id')
        ->where('superior_id', $codigoSuperior_id)
        ->groupBy('modulo_id')            
        ->select(DB::raw('count(turma) qtdeturmas, sum(matriculado) qtdematriculados, count(desistiu) qtdedesistiu, modulo_id'))
        ->get();
    
17.08.2014 / 01:56