ItSolutionStuff.com

How to Get Month Wise Data in Laravel?

By Hardik Savani • April 16, 2024
Laravel

Hi,

I am going to explain you example of how to get month wise data in laravel. you can see get month wise data in laravel. you can see how to get month wise record in laravel. This article goes in detailed on laravel get month wise data example. Here, Creating a basic example of laravel group by month and sum.

we can get month wise records in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 application.

Here, i will give you very simple example of how to get month wise record in laravel. we will use MONTHNAME() and groupBy() in this example.

I have one created items table that structure as bellow you can see.

items table:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\Http;

use App\Models\Item;

use Carbon\Carbon;

use DB;

class ITSController extends Controller

{

/**

* Write code on Method

*

* @return response()

*/

public function index()

{

$items = Item::select(

DB::raw("(COUNT(*)) as count"),

DB::raw("MONTHNAME(created_at) as month_name")

)

->whereYear('created_at', date('Y'))

->groupBy('month_name')

->get()

->toArray();

dd($items);

}

}

Output:

Array

(

[0] => Array

(

[count] => 8

[month_name] => August

)

[1] => Array

(

[count] => 4

[month_name] => October

)

)

I hope it can help you...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Get Current Week Records in Laravel?

Read Now →

Laravel Eloquent without() and withOnly() Method Example

Read Now →

Laravel Get Next and Previous Record with URL Example

Read Now →

How to Restore Deleted Records in Laravel?

Read Now →

Laravel Copy Record using Eloquent Replicate Example

Read Now →

How to Get Last 7 Days Record in Laravel?

Read Now →

How to Get Last 30 Days Record in Laravel?

Read Now →

Laravel Eloquent Delete Record By ID Example

Read Now →

Delete All Records from Table in Laravel Eloquent

Read Now →

How to Get Current Month Records in Laravel?

Read Now →

How to Get Soft Deleted Records in Laravel?

Read Now →