0

Hey guys Im new to laravel. I have this table

enter image description here

How will i able to fetch count of 'is_correct' column for each subject?

Thanks guys!

JSTR
  • 131
  • 13

1 Answers1

1

Try this, replace the Model with your eloquent model name

Model::select(
'subject_id',
 Illuminate\Support\Facades\DB::raw("COUNT(is_correct) as count")
)
->groupBy('subject_id')
->get();
Kenath
  • 600
  • 5
  • 13
  • hey man tried this but no luck it gives me a database error 1055 thanks! – JSTR Jan 22 '21 at 13:11
  • 1
    Can you share the error message? and the code you tried? – Kenath Jan 22 '21 at 13:12
  • 1
    I've edited the answer. Please check whether it's working – Kenath Jan 22 '21 at 13:17
  • Hey dude @Kenath. I think it works now and had a syntax error. This is a great help thanks a lot. Anyway I also find this very similar if someone need it. [similar_scenario](https://stackoverflow.com/questions/18533080/laravel-eloquent-groupby-and-also-return-count-of-each-group) – JSTR Jan 22 '21 at 13:19