troubleshooting Question

Could you point how to adjust this Laravel Eloquent query to remove the  `  at   `2019`  ?

Avatar of Eduardo Fuerte
Eduardo FuerteFlag for Brazil asked on
LaravelPHP
6 Comments1 Solution75 ViewsLast Modified:
Hi Experts

Could you point how to adjust this Laravel Eloquent query to remove the  ` at `2019`?

Accordingly with:
 $metas = XXXXXXX
            ::rightjoin(\DB::raw('(
                SELECT
                    distribuidores.id as distribuidor_id
                    ,distribuidores.cnpj
                    ,distribuidores.razaosocial
                    ,meses.mes
                    ,meses.nomeMes
                    ,meses.abreviatura
                    ,periodos.id as periodo_id
                    ,periodos.mes as periodo_mes
                    ,periodos.ano
                FROM distribuidores, meses, periodos
            ) as temp'), function($join) use($ano){
                $join->on('distribuidormetas.distribuidor_id', 'temp.distribuidor_id')
                     ->on('distribuidormetas.periodo_id', 'temp.periodo_id')
                     ->on('temp.mes', 'temp.periodo_mes')
                     ->on('temp.ano', (int)$ano);
            })
            ->where(function($query) use($distribuidor_id){
                if(!empty($distribuidor_id)){
                    $query->where('temp.distribuidor_id', \DB::raw($distribuidor_id));
                }
            })
            ->orderBy('temp.razaosocial', 'asc')
            ->orderBy('temp.mes', 'asc')
            ->select([
                'temp.distribuidor_id',
                'temp.cnpj',
                'temp.razaosocial',
                'temp.mes',
                'temp.nomeMes',
                'temp.abreviatura as mesAbreviado',
                \DB::raw('ifnull(distribuidormetas.valor, 0) as meta')
            ])
            ->distinct()
            ->toSql();




The problem remains

Produces:

select distinct `temp`.`distribuidor_id`, `temp`.`cnpj`, `temp`.`razaosocial`, `temp`.`mes`, `temp`.`nomeMes`, `temp`.`abreviatura` as `mesAbreviado`, ifnull(distribuidormetas.valor, 0) as meta from `distribuidormetas` right join (
                SELECT
                    distribuidores.id as distribuidor_id
                    ,distribuidores.cnpj
                    ,distribuidores.razaosocial
                    ,meses.mes
                    ,meses.nomeMes
                    ,meses.abreviatura
                    ,periodos.id as periodo_id
                    ,periodos.mes as periodo_mes
                    ,periodos.ano
                FROM distribuidores, meses, periodos
            ) as temp on `distribuidormetas`.`distribuidor_id` = `temp`.`distribuidor_id` and `distribuidormetas`.`periodo_id` = `temp`.`periodo_id` and `temp`.`mes` = `temp`.`periodo_mes` and `temp`.`ano` = `2019` order by `temp`.`razaosocial` asc, `temp`.`mes` asc


Even changing to:

 //->on('temp.ano', (int)$ano);
 ->on('temp.ano', 2019);

The problem remains.

Thanks in advance!
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros