簡體   English   中英

違反完整性約束:1052 where子句不明確的列'prof_id'Laravel

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM