Eloquent Count + Group By + Inner join

0

I am having some problems with the queries in the ORM-Eloquent, I have integrated eloquent in codeigniter, I am trying to make the following query:

SELECT tp.*, COUNT(*) as cuenta
FROM tipo_llamadas tp
INNER JOIN encuestas e ON tp.id = e.id_tipo_llamada
WHERE e.id_oleada = 1
GROUP BY tp.id

The result is the following:

id  | nombre       | cuenta
---------------------------
1   | Información  | 3
2   | Recogida     | 1

But I can not get it out, I have taken the following query, but I need to get the INNER JOIN with tipo_llamadas to get their names:

Encuesta::selectRaw('*, count(*) as cuenta')
           ->groupBy('id_tipo_llamada')
           ->get()
           ->toArray();
    
asked by PriNcee 15.12.2016 в 16:52
source

1 answer

1

In eloquent you have join () to do inner join

Modelo_tipo_llamadas::selectRaw('tipo_llamadas.*, count(*) as cuenta')
            ->join('encuestas e', 'tipo_llamadas.id', '=', 'e.id_tipo_llamada')
            ->where('e.id_oleada', '=', 1)
            ->groupBy('tipo_llamadas.id')
            ->get();

You can also build the query without the model:

$var = DB::table('tipo_llamadas tp')
        ->selectRaw('tp.*, count(*) as cuenta')
        ->join('encuestas e', 'tp.id', '=', 'e.id_tipo_llamada')
        ->where('e.id_oleada', '=', 1)
        ->groupBy('tp.id')
        ->get();
    
answered by 27.08.2017 в 18:39