Mysql 如何在Laravel中对特定用户使用count()进行选择?
以下是我的数据库结构: 此外,还有一个表users和receiver_id引用该表id。 我使用该查询从notification_types表中获取每种类型通知的计数以及该类型通知的数据Mysql 如何在Laravel中对特定用户使用count()进行选择?,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,以下是我的数据库结构: 此外,还有一个表users和receiver_id引用该表id。 我使用该查询从notification_types表中获取每种类型通知的计数以及该类型通知的数据 Notification:: select('notification_types.*', DB::raw('count(*) as total')) ->join('notification_types', 'notifications.type_i
Notification::
select('notification_types.*', DB::raw('count(*) as total'))
->join('notification_types', 'notifications.type_id', '=', 'notification_types.id')
->groupBy('notifications.type_id')
->get()
我需要的是对receiver\u id设置约束,我只是不明白-我应该把where子句放在哪里 只需将
where
方法链接到get
之前的任何位置,因为您的条件将应用于通知表:
Notification::select('Notification_types.*',DB::raw('count(*)as total'))
->join('notification_types'、'notification.type_id'、'='、'notification_types.id')
->其中('receiver\u id',$receiverId)
->groupBy('type\u id')
->get();
此外,此查询不需要按通知分组。键入\u id
,键入\u id
即可,因为此处没有创建歧义,因为没有其他名为type\u id
的列该死,我在->选择()之前插入了->where(),为什么它不起作用,我必须把它放在groupBy之前?实际上,以什么顺序链接方法并不重要,因为Laravel查询生成器确保它以正确的顺序构建查询字符串。所以你可能第一次错过了什么:)。