Hello, I have a somewhat complex query which I need to migrate to Eloquent with Laravel or QueryBuilder. The query is as follows:
SELECT tipo, id_banco, nro_operacion, anno,
sum(CASE WHEN mes = 1 THEN cuota ELSE 0 END) AS Enero,
sum(CASE WHEN mes = 2 THEN cuota ELSE 0 END) AS Febrero,
sum(CASE WHEN mes = 3 THEN cuota ELSE 0 END) AS Marzo,
sum(CASE WHEN mes = 4 THEN cuota ELSE 0 END) AS Abril,
sum(CASE WHEN mes = 5 THEN cuota ELSE 0 END) AS Mayo,
sum(CASE WHEN mes = 6 THEN cuota ELSE 0 END) AS Junio,
sum(CASE WHEN mes = 7 THEN cuota ELSE 0 END) AS Julio,
sum(CASE WHEN mes = 8 THEN cuota ELSE 0 END) AS Agosto,
sum(CASE WHEN mes = 9 THEN cuota ELSE 0 END) AS Septiembre,
sum(CASE WHEN mes = 10 THEN cuota ELSE 0 END) AS Octubre,
sum(CASE WHEN mes = 11 THEN cuota ELSE 0 END) AS Noviembre,
sum(CASE WHEN mes = 12 THEN cuota ELSE 0 END) AS Diciembre
from (
SELECT amortizacion.tipo, b.id as id_banco, amortizacion.nro_operacion,
MONTH(amortizacion_detalle.fecha_vencimiento) AS `mes`,
YEAR(amortizacion_detalle.fecha_vencimiento) AS `anno`,
Sum(amortizacion_detalle.valor_cuota) AS cuota
FROM
amortizacion_detalle
LEFT JOIN amortizacion ON amortizacion_detalle.amortizacion_id = amortizacion.id
LEFT JOIN banco AS b ON amortizacion.banco_id = b.id
WHERE
amortizacion.cliente_id = 10
GROUP BY
amortizacion_detalle.fecha_vencimiento
ORDER BY anno) as resultados
GROUP BY tipo, id_banco, nro_operacion, anno
But what is needed is to be able to do it in Laravel by bringing or improving the result of said query.
$facs = DB::table('amortizacion_detalle')
->select(DB::raw('tipo, id_banco, nro_operacion, anno,
sum(CASE WHEN mes = 1 THEN cuota ELSE 0 END) AS Enero,
sum(CASE WHEN mes = 2 THEN cuota ELSE 0 END) AS Febrero,
sum(CASE WHEN mes = 3 THEN cuota ELSE 0 END) AS Marzo,
sum(CASE WHEN mes = 4 THEN cuota ELSE 0 END) AS Abril,
sum(CASE WHEN mes = 5 THEN cuota ELSE 0 END) AS Mayo,
sum(CASE WHEN mes = 6 THEN cuota ELSE 0 END) AS Junio,
sum(CASE WHEN mes = 7 THEN cuota ELSE 0 END) AS Julio,
sum(CASE WHEN mes = 8 THEN cuota ELSE 0 END) AS Agosto,
sum(CASE WHEN mes = 9 THEN cuota ELSE 0 END) AS Septiembre,
sum(CASE WHEN mes = 10 THEN cuota ELSE 0 END) AS Octubre,
sum(CASE WHEN mes = 11 THEN cuota ELSE 0 END) AS Noviembre,
sum(CASE WHEN mes = 12 THEN cuota ELSE 0 END) AS Diciembre'))
->where('amortizacion.cliente_id', '=', $request->input('cliente_id'))
->get();
Your query would look like this if you use the Query Builder :
The only new thing would be the use of
fromSub($query, $alias)
that was a method added in the version5.6
(I don't know what version of laravel you are using).The query speaks for itself, the logic is the same.
First you create the subquery, which will be where you will get the data from, after that, you do a select() to get the columns from the previous subquery and do what you had already defined.
That query is too long to validate, and you will slow down the system, better take them to a
procedimiento almacenado (store procedure)
Something like this
you invoke from Laravel
$report would have your list of data