Laravel 5 Carbon Format
Edit: Thanks to Mr. Davidson's comment I changed my query to this:
$query->where(DB::raw('MONTH(date)'), '=', Carbon::parse($month)->format('m'));
In my expenses overview I have a dropdown where I can select a month which then queries only the results for that particular month, like so:
$query->where('date', 'LIKE', '%' . Carbon::parse($month)->format('m') . '%');
Before, my query was like this:
$query->where('date', 'LIKE', '%2015-' . Carbon::parse($month)->format('m') . '%');
In my database I have a datetime field, so the above works, but ofcourse it only shows results from 2015. I am trying to figure out how to use the first query to show results from 2016 also.
Instead of using
LIKE, you can use the
$query->whereMonth('date', '=', Carbon::parse($month)->format('m'));
Because it's much easier to read. The method is not documented in the Laravel Query Builder Docs, but you can check out the Query Builder API to see the method signature, as well as to check out what other methods are available for use when building queries (since there are quite a few that are not being described in the official documentation).
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?