mysql - How can I select with count() on specific user in Laravel? -


here database structure: db structure

also - there table users , reciever_id references table id. use query count of each type of notifications data type of notifications notification_types table.

notification::              select('notification_types.*', db::raw('count(*) total'))              ->join('notification_types', 'notifications.type_id', '=', 'notification_types.id')              ->groupby('notifications.type_id')              ->get() 

what need - set constraint on reciever_id, don't - should put clause?

just chain where method anywhere before get, since condition applied on notifications table:

notification::select('notification_types.*', db::raw('count(*) total'))     ->join('notification_types', 'notifications.type_id', '=', 'notification_types.id')     ->where('reciever_id', $receiverid)     ->groupby('type_id')     ->get(); 

also, there's no need query group notifications.type_id, type_id do, because there no ambiguities created here because there no other columns named type_id.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - Bypass Geo Redirect for specific directories -

php - .htaccess mod_rewrite for dynamic url which has domain names -