Throughout this Laravel Eloquent whereMonth query example; you will learn how to use whereMonth() query builder in laravel. The whereMonth method may be used to compare a column’s value against a specific month.
If you want to get a specific month records in laravel then you just use the whereMonth query. You can use it in your laravel 5, laravel 6, laravel 7 laravel 8, or laravel 9 apps.
Example 1:
Get the specific month recrods to compare created using whereMonth query;
use App\Models\User;
class UserController extends Controller
{
public function index()
{
$month = '05';
$users = User::whereMonth('created_at', '=', $month)
->get();
dd($users);
}
}
Example 2:
Here in this example get the specific month and above records using where month query in laravel;
use App\Models\User;
class UserController extends Controller
{
public function index()
{
$month = '05';
$users = User::whereMonth('created_at', '>=', $month)
->get();
dd($users);
}
}
I hope the laravel eloquent whereMonth query example work for you.