Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 如何在Laravel中对特定用户使用count()进行选择?_Mysql_Laravel_Eloquent - Fatal编程技术网

Mysql 如何在Laravel中对特定用户使用count()进行选择?

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

以下是我的数据库结构:

此外,还有一个表users和receiver_id引用该表id。 我使用该查询从notification_types表中获取每种类型通知的计数以及该类型通知的数据

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查询生成器确保它以正确的顺序构建查询字符串。所以你可能第一次错过了什么:)。