Skip to content
Advertisement

How to combine two counts using Laravel

Using Laravel 8 for a restful API. A table in the database has following fields

report_date , supervisor_id and supervisor_note

I want to retrieve a result that counts the number of reports on any given day date and also counts all of the null values in supervisor_notes field.

I am using the following Laravel code:

JavaScript

I don’t know how to combine the code with the second count condition in the same query

Advertisement

Answer

You can use conditional aggregation here

JavaScript

Also if supervisor_id is integer column and $supervisor_id also holds integer value then use exact integer matching like = instead of using string comparison using like clause

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement