ItSolutionStuff.com

Laravel Select with Count Query with Group By Example

By Hardik Savani β€’ November 5, 2023
Laravel

Hi,

In this profound tutorial, we will learn laravel select count group by. I would like to share with you laravel select count group by. let’s discuss about laravel raw query count. we will help you to give an example of laravel query count rows. Let's get started with laravel count records.

We always required to get count of number of records or number of inserted record of current month etc in laravel. We can use mysql count function in laravel eloquent. We have two way to get count of column value. first we can use laravel count() of query builder and another one we can use with directly with select statement using DB::raw(). I give you both example you can see and use any one as perfect for you.

Example:

Controller Code:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Click;

use DB;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index(Request $request)

{

$data = Click::select("id", DB::raw("COUNT(*) as count_row"))

->orderBy("created_at")

->groupBy(DB::raw("year(created_at)"))

->get();

dd($data);

}

}

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

β˜…

Laravel Carbon Count Weekends Days Between Two Dates Example

Read Now β†’
β˜…

Laravel Carbon Count Working Days Between Two Dates Example

Read Now β†’
β˜…

Laravel Carbon Count Days Between Two Dates Example

Read Now β†’
β˜…

Laravel Where Clause with Function Query Example

Read Now β†’
β˜…

Laravel Sum Query with Where Condition Example

Read Now β†’
β˜…

Laravel Eloquent whereRelation() Condition Example

Read Now β†’
β˜…

Laravel Eloquent whereHas() Condition Example

Read Now β†’
β˜…

Laravel Groupby Having with DB::raw() Example

Read Now β†’
β˜…

Laravel Group By with Month and Year Example

Read Now β†’
β˜…

Laravel Eloquent Group By with Multiple Columns Example

Read Now β†’
β˜…

How to use DB Transactions in Laravel?

Read Now β†’
β˜…

Laravel Join with Subquery in Query Builder Example

Read Now β†’
β˜…

GROUP_CONCAT with different SEPARATOR in Laravel Example

Read Now β†’