Sometimes you may face such a condition that you need to fetch current month records in Laravel but you do not know how to get current month records in Laravel. There are many ways to fetch current month records in Laravel.

In this tutorial, I will show you how to get the current month count in Laravel and create a report like a Laravel monthly report using this current month fetching data.

I will show you an example query of how you select a query for the current month. Let's see the example query of how to get current month records in Laravel:

App\Http\Controllers\TutorialController.php

<?php

namespace App\Http\Controllers;

use Carbon\Carbon;
use App\Models\Record;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TutorialController extends Controller
{
    public function __invoke(Request $request)
    {   
        $records = Record::whereBetween(
                DB::raw('DATE(billing_date)'),
                [
                    Carbon::now()->startOfMonth(),
                    Carbon::now()->endOfMonth()
                ]
            )->sum('net_bill');

        return $records;
    }
}

 

This code will retrieve all records from the "records" table where the "billing_date" date falls between the start and end of the current month.

 

Read also: How To Fetch This Week Records In Laravel?

 

Conclusion

In this blog post, we've learned how to fetch records from a database using Laravel that belong to the current month. By utilizing PHP carbon date functions, you can easily filter and retrieve the data you need. Hope this how to get current month count in laravel tutorial will help you.

Category : #laravel

Tags : #laravel , #laravel query