简体   繁体   English

违反完整性约束:1052 where子句不明确的列'prof_id'Laravel

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

相关问题 Laravel 完整性约束违规:1052 列 'id' in where 子句不明确 - Laravel Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 完整性约束违规:1052 where子句中的列&#39;id&#39;不明确 - Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel Eloquent:违反完整性约束:where 子句中的 1052 列“id”不明确 - Laravel Eloquent: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous 如何解决完整性约束违规:1052 列 &#39;id&#39; in where 子句在 laravel 中不明确 - How to solve Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous in laravel Laravel 6 错误:SQLSTATE[23000]:违反完整性约束:1052 where 子句中的列“id_perusahaan”不明确 - Laravel 6 Error : SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id_perusahaan' in where clause is ambiguous Laravel SQL 错误:违反完整性约束:1052 where 子句中的列“id”不明确 - Laravel SQL Error: Integrity constraint violation: 1052 Column 'id' in where clause is ambiguous Laravel Eloquent SQLSTATE[23000]:违反完整性约束:1052 列...在 where 子句中不明确 - Laravel Eloquent SQLSTATE[23000]: Integrity constraint violation: 1052 Column ... in where clause is ambiguous 完整性约束违规:1052 列 'group_id' 在 where 子句不明确 - Integrity constraint violation: 1052 Column 'group_id' in where clause is ambiguous 违反完整性约束:1052 order子句中的“位置”列不明确 - Integrity constraint violation: 1052 Column 'position' in order clause is ambiguous SQLSTATE [23000]:违反完整性约束:1052 where 子句中的列“值”不明确 - SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'value' in where clause is ambiguous
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM