How to Get All Month Record Count in Laravel

How to get all month record count in laravel

Please try the below steps:-

  1. Run composer to install package: composer require nesbot/carbon
  2. On top of your code: use Carbon\Carbon;

Suppose i have Model User,

$users = User::select('id', 'created_at')
->get()
->groupBy(function($date) {
//return Carbon::parse($date->created_at)->format('Y'); // grouping by years
return Carbon::parse($date->created_at)->format('m'); // grouping by months
});

$usermcount = [];
$userArr = [];

foreach ($users as $key => $value) {
$usermcount[(int)$key] = count($value);
}

for($i = 1; $i <= 12; $i++){
if(!empty($usermcount[$i])){
$userArr[$i] = $usermcount[$i];
}else{
$userArr[$i] = 0;
}
}

Hope it will help you to make array like that.

get row count according to the month in the date in Laravel

You should be able to achieve this what you're after with:

$data = \App\SaleData::selectRaw('COUNT(*) as count, YEAR(dateOfSale) year, MONTH(dateofSale) month')
->groupBy('year', 'month')
->get();

If you don't include the year as well then you'll have sales from different years included in the same month e.g. sales from October 2017 and October 2018 will be included together.

How do i get count of records for each month in an array

Retrieve data only once from the database

public function clientsCount(Request $request)
{
$selectedYear = $request->selectedYear ?? now()->format('Y');

$data = Client::selectRaw('count(id) as count, month(created_at) as month')
->whereYear('created_at', $selectedYear)
->groupBy('month')
->pluck('count', 'month');

$period = CarbonPeriod::create(
now()->setYear($selectedYear)->startOfYear(), '1 month', now()->setYear($selectedYear)->endOfYear()
);

$orderedMonthsData = [];

foreach ($period as $key => $date) {
$orderedMonthsData[++$key] = $date->format('F Y');
}

$orderedClientsData = [];

foreach ($orderedMonthsData as $month => $text) {
$orderedClientsData[$month] = $data->get($month) ?? 0;
}

// dd($orderedMonthsData, $orderedClientsData);
}

You don't have to create orderedMonthsData manually. You can make it with a code what I provided above for you.

Laravel monthly count record

You can try something like this:

$year = [0,0,0,0,0,0,0,0,0,0,0,0];//initialize all months to 0

foreach($monthly_uploaded_product as $key)
$year[$key->month-1] = $key->total;//update each month with the total value
}

Query Count Monthly of this year - Laravel 5.8

you should extract month from date then groubby ,and don't forgot select count :

    Visitor::whereYear('created_at', Carbon::now()->year)
->select(DB::raw("MONTH(created_at) month"),DB::raw("count('month') as vistors_count"))
->groupby('month')
->get();

Laravel Eloquent get sale count for every day of a month

the groupby clause should include all the columns you select, since you did not use 'select' statement then all columns is selected, witch conflict with this rule.

you should use select, or selectRaw to strictly identify your selected columns,
and you can use DAYOFMONTH function to get the day of the month:

 $salesData = Sale::whereBetween('created_at', [Carbon::createFromDate($year, $month, 1), Carbon::createFromDate($year, $month, $daysCount)])
->selectRaw('DAYOFMONTH(created_at) monthDay,count(*)')
->groupBy('monthDay')
->get();

Eloquent: get data for every day of the month

you can transform the data like this

$brandsMonthlyInfo = array();
$dates = collect();

foreach( range( -30, 0 ) AS $i ) {
$date = Carbon::now()->addDays( $i )->format( 'Y-m-d' );
$dates->put( $date, 0);
}

$countAddedBrands = DB::table('Clubs')->where([['created_at', '>=', $dates->keys()->first()]])
->groupBy( 'date' )
->groupBy( 'brand' )
->orderBy( 'date' )
->get( [
DB::raw( 'DATE( created_at ) as date' ),
DB::raw( 'COUNT(brand) AS "count"' ),
'brand'
] );

// create the array to contain the objects
$arrayOfAddedBrands = [];
foreach($countAddedBrands as $brand){
$found = 0;
// looping over the array to search for the date if it exists
foreach($arrayOfAddedBrands as $key => $brandFromArray ){
// if the date exists
if($brand->date == $brandFromArray->date){
// add the brand to the clubs object
$brandFromArray->clubs->{$brand->brand} = $brand->count;
$found = 1;
// assign the array element to the new modified object
$arrayOfAddedBrands[$key] = $brandFromArray;
break;
}
}
// if the date not found
if(!$found){
// create new object and assign the date to it
$brandObject = new \stdClass;
$brandObject->date = $brand->date;
// creating the clubs object
$brandObject->clubs = new \stdClass;
$brandObject->clubs->{$brand->brand} = $brand->count;
// adding the final object to the array
$arrayOfAddedBrands[] = $brandObject;
}
}

return $arrayOfAddedBrands;


Related Topics



Leave a reply



Submit