[英]Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous
[英]Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel
我想做的事情很简单,运行具有某些关系的查询并为该关系提供where子句。该查询假定要获取具有相关关系的问题,但标签上的where子句只会告诉获得与标签相关的问题已发送($ value)。
userQuestion模型 :
<?php
namespace App;
class userQuestion extends Model
{
protected $table = "question";
public $primaryKey = "question_id";
protected $fillable = ['question_id'];
public function gettags() {
return $this->belongsToMany('App\Profskills','question_profskill',"question_id","prof_id");
}
}
询问
$allquestions = userQuestion::with('getanswer','getusername','getbounty','gettags')
->whereHas('gettags', function($q) use($value) {
$q->where('prof_id', '=', $value);
})
->orderBy('created_at','Desc')
->get();
问题是它给了我这个错误
QueryException in Connection.php line 729:
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'prof_id' in
where clause is ambiguous (SQL: select * from `question` where exists
(select * from `prof_skills` inner join `question_profskill` on
`prof_skills`.`prof_id` = `question_profskill`.`prof_id` where `question_profskill`.`question_id` = `question`.`question_id` and
`prof_id` = 1) order by `created_at` desc)
列存在,如果我将列切换为qp_id (PrimaryKey),它将起作用,并且列来自即时通讯尝试访问的数据透视表
做了一些谷歌搜索,我所做的是:
在模型中用'prof_id'填充1次(因为我也有数据透视表的模型,做了同样的事情)
2次尝试=> where而不是wherehas
仍然卡住
谢谢你的帮助!
在两个表中都有prof_id时,在字段中添加表名称。
$allquestions = userQuestion::with('getanswer','getusername','getbounty','gettags')
->whereHas('gettags', function($q) use($value) {
$q->where('question_profskill.prof_id', '=', $value); //question_profskill or prof_skills
})
->orderBy('created_at','Desc')->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.