与...一起工作Laravel 7.x
and Postgresql
。我有一个User
类和GroupPivot
数据透视表,可以对多个模型进行分组。我想创建一个专栏User
如果用户拥有针对该组的当前数据透视条目,则为 true 或 false 类。这样我就可以列出所有用户并知道他们是在组内还是在组外。
我的查询:
User::addSelect([
'in_group' => GroupPivot::whereColumn('groups_pivot.pivot_id', 'users.id')
->where('groups_pivot.group_id', $group->id)
->where('groups_pivot.model', User::class)
->count()
])->get();
我已经被困有一段时间了:
SQLSTATE[42P01]: Undefined table: 7 ERROR: missing FROM-clause entry for table "users" LINE 1: ... "groups_pivot" where "groups_pivot"."pivot_id" = "users"."i... ^ (SQL: select count(*) as aggregate from "groups_pivot" where "groups_pivot"."pivot_id" = "users"."id" and "groups_pivot"."model" = App\Models\User)
我错过了什么?谢谢。
这解决了它。它不喜欢->count()
.
User::addSelect([
'in_group' => GroupPivot::selectRaw('COUNT(*)')
->whereColumn('groups_pivot.pivot_id', 'users.id')
->where('groups_pivot.group_id', $group->id)
->where('groups_pivot.model', User::class)
])->get();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)