Picking up monthly items ELOQUENT MODEL

Asked

Viewed 236 times

2

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

In the query I’ve done I’m bringing them all with status O of OK:

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

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

  • You’re wearing the Laravel 5.3?

  • 1

    No, I’m using the 5.2

1 answer

2


In the version until 5.2 use the 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 version 5.3 use the new method whereMonth:

$mes = date('m');

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

Remarks: in the whereRaw any existing function in the bank can be used, for example in the bank link to the SOPT, owns the SQL Date, month and year filters obtained from a field date or datetime in Banco . I must also point out that the whereRaw works in current versions of .

References:

  • 1

    You are simply the best kkk vlw

  • Thank you, but I’m mere mortal just trying to help spread technology...!

  • Just to ask a question, if I want year, and day there are also auxiliary functions ? In case it’s not too much to ask and in order to increase the response with extra content, do not run away from the Stackoverflow standards. It would be of great value to the community.

  • 1

    @Renanrodrigues I made an edit and put a link that references the filters of a date as was said by you in your comment. It’s the same process, now just put SQL according to what you need, was that?

  • 1

    Just remembering that each bank has its own particular functions and with whereRaw it is possible to program to banks that already have implementation in Laravel.

Browser other questions tagged

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