How to Get Current Month Records in Laravel
In this lesson, we will see how to retrieve only current month records from the database in Laravel, let's assume that we have an e-commerce website and we want to get the orders done for the current month.
Get current month orders
So to get current month orders we use the following eloquent model query:
<?php
namespace App\Http\Controllers\Admin;
use Carbon\Carbon;
use App\Models\Order;
use App\Http\Controllers\Controller;
class AdminController extends Controller
{
/**
* Get current month orders
*/
public function index()
{
return view('admin.index')->with([
'monthOrders' => Order::whereMonth('created_at', Carbon::now()->month)->get()
]);
}
}