违反完整性约束:1052 列 'prof_id' 在 where 子句中不明确 Laravel
Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel
我想做的很简单,运行 一个有一些关系的查询,并给那个关系一个 where clause.the 查询假设得到有关相关关系的问题,但是 where 子句on tags 告诉只获取带有已发送标签的问题($value)。
用户问题模型:
<?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),它将起作用并且 Columns 是来自我试图访问的枢轴table
谷歌搜索了一下,我所做的是:
1-在模型中用 'prof_id' 填充(因为我也有一个枢轴模型 table,做了同样的事情)
2-try =>where 而不是 whereHas
仍然卡住了,
感谢您的帮助!
在您的字段中添加 table 名称,因为您在两个 table 中都有 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();
我想做的很简单,运行 一个有一些关系的查询,并给那个关系一个 where clause.the 查询假设得到有关相关关系的问题,但是 where 子句on tags 告诉只获取带有已发送标签的问题($value)。
用户问题模型:
<?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),它将起作用并且 Columns 是来自我试图访问的枢轴table
谷歌搜索了一下,我所做的是:
1-在模型中用 'prof_id' 填充(因为我也有一个枢轴模型 table,做了同样的事情)
2-try =>where 而不是 whereHas
仍然卡住了,
感谢您的帮助!
在您的字段中添加 table 名称,因为您在两个 table 中都有 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();