Create query in query builder - laravel

0

I'm starting to work with Laravel and I need to make a query in the database, it's a simple query, but as I'm starting I still do not know how to do it.

I need to get the sales made today, yesterday, this month, previous month, current year and previous year , but first I need to make this query work in Laravel, builder .

SELECT 
    SUM(TotalVenda) as TotalVenda 
FROM vendas 
WHERE DATE(dtRegistro) = CURDATE()

I'm having difficulty joining syntax statements, for example:

$datas = DB::table('vendas')
    ->whereDate('dtRegistro', Carbon::now()->format('m/d/Y'))
    ->get();

After that I need to get the sum of the result obtained, but in this example even it no longer returns the expected date records that would be today.

    
asked by anonymous 14.09.2018 / 22:45

1 answer

0

I was able to solve it, it would have to be done with whereRaw, to bring the query that I had made in SQL to Laravel.

$total_hoje = Vendas::whereRaw('date(DataEmissao)=date(NOW())')
                                        ->whereIn('tipo', ['Devolução', 'Pedido de venda', 'Troca'])
                                        ->sum('totalvenda');

$total_ontem = Vendas::whereRaw('date(DataEmissao)=date(DATE_ADD(now(), INTERVAL -1 DAY))')
                                        ->whereIn('tipo', ['Devolução', 'Pedido de venda', 'Troca'])
                                        ->sum('totalvenda');
    
15.09.2018 / 15:24