【发布时间】:2018-04-24 00:44:31
【问题描述】:
我有一个通知表(和模型)
通知表列因此是:
id
title
body
is_public
...
我还有一个用户表(和模型)
用户表列:
id
username
...
我还有一个数据透视表 notification_user 表
列:
user_id
notification_id
在 Notification 和 User 模型上都设置了多对多关系,因此:
Notification.php
public function users()
{
return $this->belongsToMany('App\Api\V1\Models\User');
}
用户.php
public function notifications()
{
return $this->belongsToMany('App\Api\V1\Models\Notification');
}
现在在Notification.php 中,我想设置一个范围。在范围内我需要获取公共通知和当前用户的
单个 SQL 查询中的私人通知。从我的表结构来看,公共通知在 is_public == 1 处。私人通知与数据透视表相关联。
为了实现这一点,在我的Notification.php 中,我也有这个设置:
public function scopePublicAndPrivate(Builder $query)
{
return $this->public($query)->union($this->private($query));
}
public function scopePublic(Builder $query)
{
return $query->where('is_public', 1);
}
public function scopePrivate(Builder $query)
{
$user = JWTAuth::parseToken()->authenticate(); //using JWT to get a user.
return $user->notifications();
}
现在,当我在控制器中尝试 Notification::publicAndPrivate()->get() 时,我得到:
Illuminate\Database\QueryException with message 'SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns (SQL: (select * from `notifications` where `is_public` = 1) union (select * from `notifications` inner join `notification_user` on `notifications`.`id` = `notification_user`.`notification_id` where `notification_user`.`user_id` = 1))
如果能帮我解决这个问题或提供更好的解决方案,我将不胜感激。
【问题讨论】:
标签: php laravel eloquent union query-builder