Call relationship with WITH no Laravel with SUM()

Asked

Viewed 22 times

0

Good morning, I’m having problems to call the next relationship in Aravel being that it has a Function that makes a sum() and group by and does not have the field of the relation one to one, after notesFatura would have another relation one to one:

$FaturasTransportadoraObj = NotasTransportadoraHeader::select('transportadora_cnpj', 'documento_cobranca', DB::raw('sum(valor_total) as valor_total, count(documento_cobranca) as documento_quantidade'))
        ->with(['notasFatura' => function($query) use ($campo){
            if(!empty($campo['uf_destinatario'])){
                $query->where('uf_destinatario', $campo['uf_destinatario']);
            }
            $query->select('documento_cobranca_header',DB::raw('sum(peso_nota) as peso_total, sum(valor_nota) as valor_total, count(numero_nota) as nota_quantidade,
            sum(valor_frete) as total_frete'))
            ->groupBy('documento_cobranca_header');
        }])
        ->with('nomeTransportadora')
        ->groupBy('transportadora_cnpj', 'documento_cobranca');

Does anyone know how to get around without creating another relationship? Thank you.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.