[英]Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel
What I'm trying to do is very simple, Running a query with some relations and giving that relation a where clause.the query suppose to get questions with the related relations BUT the where clause on tags tells only get questions with the tag that been sent ($value). 我想做的事情很简单,运行具有某些关系的查询并为该关系提供where子句。该查询假定要获取具有相关关系的问题,但标签上的where子句只会告诉获得与标签相关的问题已发送($ value)。
userQuestion Model : 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");
}
}
Query 询问
$allquestions = userQuestion::with('getanswer','getusername','getbounty','gettags')
->whereHas('gettags', function($q) use($value) {
$q->where('prof_id', '=', $value);
})
->orderBy('created_at','Desc')
->get();
the problem is it gives me this error 问题是它给了我这个错误
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)
the column exist and if i switch the column to qp_id (PrimaryKey) it will work and the Columns are from the pivot table that im trying to access 列存在,如果我将列切换为qp_id (PrimaryKey),它将起作用,并且列来自即时通讯尝试访问的数据透视表
Did some googling, what i did was : 做了一些谷歌搜索,我所做的是:
1-put fillable with 'prof_id' in the model ( since i have a model for the pivot table too , did the same thing) 在模型中用'prof_id'填充1次(因为我也有数据透视表的模型,做了同样的事情)
2-try =>where instead of whereHas 2次尝试=> where而不是wherehas
Still stuck, 仍然卡住
Thanks for any help! 谢谢你的帮助!
Add table name with your field as you have prof_id in both tables. 在两个表中都有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.