DB::table request get only date from "created_at" field? I want to create a stat how many items are created by user today, but the problem is the "created_at" field has also a time, how can I do this request with ignoring time?
DB::table('users')
->join('items', 'users.id', '=', 'items.user_id')->select('*')
->where('items.user_id', '=', $user_id)->where('categories.created_at', '=', Carbon::now()->format('Y-m-d'))
Eloquent has built in ways to get just the year and month. Have a play around like this:
->whereYear('created_at', '=', $year)
->whereMonth('created_at', '=', $month)
->whereDay etc
Edit:
I think you can also do this:
->whereDate('created_at', '2016-12-31') //from the docs
Carbon::today() will give you a Carbon object from 00:00:00 on topday's date; then just use >= to fetch all of the records created today.
DB::table('users')
->join('items', 'users.id', '=', 'items.user_id')
->where('items.user_id', '=', $user_id)
->where('items.created_at', '>=', Carbon::today)
->get()
Thank you @mediax for the vote but @tykus 's suggestion was better (and directly answered you question :)
Please sign in or create an account to participate in this conversation.