Mysql SQLSTATE[42000]:语法错误或访问冲突:关系上的1066非唯一表/别名
因此,我从Laravel框架收到以下错误:;但我无法找到此框架产生此错误的原因:Mysql SQLSTATE[42000]:语法错误或访问冲突:关系上的1066非唯一表/别名,mysql,laravel,eloquent,laravel-4,relationship,Mysql,Laravel,Eloquent,Laravel 4,Relationship,因此,我从Laravel框架收到以下错误:;但我无法找到此框架产生此错误的原因: SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'participants' (SQL: select `participants`.*, `participants`.`message_id` as `pivot_message_id`, `participants`.`user_id` as `piv
SQLSTATE[42000]: Syntax error or access violation: 1066 Not unique table/alias: 'participants' (SQL: select `participants`.*, `participants`.`message_id` as `pivot_message_id`, `participants`.`user_id` as `pivot_user_id`, `participants`.`created_at` as `pivot_created_at`, `participants`.`updated_at` as `pivot_updated_at` from `participants` inner join `participants` on `participants`.`id` = `participants`.`user_id` where `participants`.`deleted_at` is null and `participants`.`message_id` in (2))
我的邮件/参与者关系如下所示:
公共功能参与者()
{
返回$this->belongTomany('Namespace\Modules\Email\Models\Participant','participants','message_id','user_id')->withTimestamps();
}
我试着这样称呼它:
公共函数getAllMessages()
{
返回消息::with('user')->with('participants')->get();
}
为什么我会犯这个错误?发生什么事了
编辑:包括完整型号
消息
类消息扩展了雄辩的
{
使用公布的薪酬;
使用软删除特征;
/**
*模型使用的数据库表。
*
*@var字符串
*/
受保护的$table='messages';
/**
*可以使用体量指定设置的属性。
*
*@var数组
*/
受保护的$fillable=['subject','user_id','body','status'];
/**
*应该变异为日期的属性。
*
*@var数组
*/
受保护的$dates=[“创建日期”、“更新日期”、“删除日期”];
/**
*验证规则。
*
*@var数组
*/
受保护的$rules=[
“主题”=>“必填项|最大值:255”,
“正文”=>“必需”,
];
/**
*用户关系
*
*@return\illumb\Database\elount\Relations\BelongsTo
*/
公共函数用户()
{
返回$this->belongsTo(Config::get('email.user_model');
}
公共职能资产()
{
返回$this->belongstomy('Namespace\Modules\Assets\Models\Asset','message_Assets');
}
/**
*参与者关系
*
*@return\illumb\Database\elount\Relations\hasmall
*/
公共活动参与者()
{
返回$this->belongTomany('Namespace\Modules\Email\Models\Participant','participants','message_id','user_id')->withTimestamps();
}
/**
*此邮件的收件人
*
*@return\illumb\Database\elount\Relations\hasmall
*/
公共职能受助人()
{
返回$this->participants()->where('user\u id','!=',$this->user\u id);
}
/**
*从线程返回最新消息
*
*@return Namespace\Modules\Email\Models\Message
*/
公共函数getLatestMessageAttribute()
{
返回$this->messages()->latest()->first();
}
/**
*返回与用户关联的线程
*@param$query
*@param$userId
*@返回混合
*/
公共函数scopeForUser($query,$userId)
{
返回$query->join('participants','messages.id','=','participants.message_id'))
->其中('participants.user_id',$userId)
->其中('participants.deleted_at',null)
->选择('messages.*');
}
/**
*返回与用户关联的线程
*@param$query
*@param$userId
*@返回混合
*/
公共函数scopeForUserWithDeleted($query,$userId)
{
返回$query->join('participants','messages.id','=','participants.message_id'))
->其中('participants.user_id',$userId)
->选择('messages.*');
}
/**
*返回用户已发送的消息
*@param$query
*@param$userId
*@返回混合
*/
公共函数scopeByUser($query,$userId)
{
返回$query->where('user\u id',$userId);
}
/**
*返回包含与用户关联的新消息的线程
*@param$query
*@param$userId
*@返回混合
*/
公共函数scopeForUserWithNewMessages($query,$userId)
{
返回$query->join('participants','messages.id','=','participants.message_id'))
->其中('participants.user_id',$userId)
->whereNull('participants.deleted_at')
->其中(函数($query){
$query->where('messages.updated_at','>',$this->getConnection()->raw($this->getConnection()->getTablePrefix()。'participants.last_read'))
->orWhereNull(“参与者。最后一次读取”);
})
->选择('messages.*');
}
}
参与者
课堂参与者扩展了口才
{
使用软删除特征;
/**
*模型使用的数据库表。
*
*@var字符串
*/
受保护的$table='参与者';
/**
*可以使用体量指定设置的属性。
*
*@var数组
*/
受保护的$fillable=['message_id','user_id','last_read'];
/**
*应该变异为日期的属性。
*
*@var数组
*/
受保护的$dates=[“创建日期”、“更新日期”、“删除日期”、“上次读取日期”];
/**
*线程关系
*
*@return\illumb\Database\elount\Relations\BelongsTo
*/
公共功能消息()
{
返回$this->hasMany('Namespace\Modules\Email\Models\Message');
}
/**
*用户关系
*
*@return\illumb\Database\elount\Relations\BelongsTo
*/
公共函数用户()
{
返回$this->belongsTo(Config::get('email.user_model');
}
}
您的错误是
...from `participants` inner join `participants` ...
您需要为每个引用提供别名,如中所示
...from `participants` p1 inner join `participants` p2 ...
然后在正确的位置使用p1
和p2
,例如
...on p1.`id` = p2.`user_id` ...
(我猜哪一个是
p1
,哪一个是p2
;你必须做出决定)通过Larachat官方网站回答:
该关系缺少一个数据透视表,因此无法正常工作。参与者
方法中的第二个参数是要使用的透视表:
public function participants()
{
return $this->belongsToMany('Namespace\Modules\Email\Models\Participant', 'PIVOT', 'message_id', 'user_id')->withTimestamps();
}
因此,您不能将参与者用作
public function messages(){
return $this->belongsToMany(Message::class, 'participants', 'user_id', 'message_id')->withPivot('last_read');
}
public function users(){
return $this->belongsToMany(User::class, 'participants', 'message_id', 'user_id')->withPivot('last_read');
}
$messages = $user->messages()->get();
$user = $message->users()->get()
$last_read = $message->pivot->last_read;