Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

masumluf's avatar

How to count query in Laravel Eloquent?

Dear Developers, I need to count and specific item from mysql using laravel eloquent query for current month. Like when i will count item it will count only for that month . but I unable to do that . I am just trying to count only but its not working too :(

$attendence= DB::table('students')
            ->join('attendences', 'attendences.student_username', '=', 'students.username')
            ->select(
            count('attendences.attendence'),
                     'attendences.created_at'
            )->where('username',$request->name)
            ->get();

I am getting error ""count(): Parameter must be an array or an object that implements Countable""

0 likes
2 replies
FareedR's avatar

you can try this . it might be error . but i guess you can find something with that code ;)

$attendance = Attendance::with('student', function ($query) {
    $query->where('username',$request->name);
})->where('created_at')->count();
Sinnbeck's avatar

Something like this

 ->selectRaw('count(attendences.attendence), attendences.created_at')

Please or to participate in this conversation.