Picking up monthly items ELOQUENT MODEL

2

Well I need to make a query where I have to bring all the listed items of the month we are in.

In query that I've done I'm bringing all with status co_de O :

$vendas = Encomenda::where('FlgStEncomenda', 'O')->count();

In my table structure I have a OK which is the creation date, made up of created_at and data as follows hora through it I need to do the search. So I have to return all 2016-11-02 16:25:03 registered this month.

    
asked by anonymous 11.11.2016 / 15:09

1 answer

2

In the to 5.2 use whereRaw :

$mes = date('m');

$vendas = Encomenda::where('FlgStEncomenda', 'O')
                   ->whereRaw("MONTH(created_at)={$mes}") 
                   ->count();

or

$vendas = Encomenda::where('FlgStEncomenda', 'O')
                   ->whereRaw("MONTH(created_at)=?", array($mes)) 
                   ->count();

In the 5.3 use the new whereMonth method:

$mes = date('m');

$vendas = Encomenda::where('FlgStEncomenda', 'O')
                   ->whereMonth("created_at",$mes) 
                   ->count();

Notes: in whereRaw can be used any function in the database, such as SOPT link , has SQL referring day, month and year filters obtained from a date or datetime field in Bank . I should also point out that whereRaw works on the current versions of .

References :

11.11.2016 / 15:43