I am working with lumen and mongodb
I want to get (total unread) and total messages count based on folder
my mongodb query is like below,
$totalEmails = DB::connection('mongodb')
->select("sum(if(is_read==0,1,0)) as unread", "count(message_id) as total", "folder_id")
->collection('email_message')
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
it gives me error as below,
FatalErrorException in Connection.php line 333:
Call to a member function prepare() on null
please help me to resolve this issue.thank you.
I have resolve that error by change my query as below,
$totalEmails = DB::connection('mongodb')
->collection('email_message')
->select("sum(if(is_read==0,1,0)) as unread", "count(message_id) as total", "folder_id")
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
but it not gives me expected result,
it give me result as below,
[_id] => Array
(
[folder_id] => 5bee461e19f043020c001844
)
[folder_id] => 5bee461e19f043020c001844
[sum(if(is_read==0,1,0)) as unread] =>
[count(message_id) as total] =>
but my expectation is
[_id] => Array
(
[folder_id] => 5bee461e19f043020c001844
)
[folder_id] => 5bee461e19f043020c001844
[unread] => 2
[total] => 10
can you tell me where is wrong in query???
If I am using below query
$totalEmails = DB::connection('mongodb')
->selectRaw("sum(if(is_read==0,1,0)) as unread", "count(message_id) as total", "folder_id")
->collection('email_message')
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
it gives me error like,
ErrorException in Builder.php line 245:
Argument 2 passed to Illuminate\Database\Query\Builder::selectRaw() must be of the type array, string given,
If I am using below query,
$totalEmails = DB::connection('mongodb')
->collection('email_message')
->selectRaw("sum(if(is_read==0,1,0)) as unread,count(message_id) as total,folder_id")
->where('email_account_id', (int)$request->email_account_id)
->where('status', "Active")
->groupBy("folder_id")
->get();
it gives me result like,
[_id] => Array
(
[folder_id] => 5bee461e19f043020c001844
)
[folder_id] => 5bee461e19f043020c001844
[sum(if(is_read==0,1,0)) as unread,count(message_id) as total,folder_id] =>
please help me to get my expected result