简体   繁体   中英

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).

userQuestion Model :

<?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

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)

2-try =>where instead of whereHas

Still stuck,

Thanks for any help!

Add table name with your field as you have prof_id in both tables.

$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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM