Add null value Laravel

1

I'm trying to do the following sum with Query Builder, but if one of the values is null and not sum

DB::raw("estudantes_carga_horaria.dias_letivos_oferecidos + estudantes_carga_horaria.dias_letivos_oferecidos_outra_escola AS total_dias_letivos_oferecidos")
    
asked by anonymous 27.06.2018 / 13:17

1 answer

1

You can use Coalesce, which will set 0 when the value is null .

DB::raw("
COALESCE(estudantes_carga_horaria.dias_letivos_oferecidos, 0) + 
COALESCE(estudantes_carga_horaria.dias_letivos_oferecidos_outra_escola, 0)
AS total_dias_letivos_oferecidos")

I'm answering what you asked, but possibly have other ways to do it depending on what you want with that value.

    
27.06.2018 / 13:23