Integrity constraint violation: 1052 Column 'prof_id' in where clause is ambiguous Laravel
up vote
0
down vote
favorite
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('AppProfskills','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!
php laravel laravel-5.2
add a comment |
up vote
0
down vote
favorite
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('AppProfskills','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!
php laravel laravel-5.2
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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('AppProfskills','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!
php laravel laravel-5.2
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('AppProfskills','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!
php laravel laravel-5.2
php laravel laravel-5.2
edited Nov 12 at 4:50
Takamura
911212
911212
asked Nov 11 at 11:16
Pc Monk
5619
5619
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
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();
Thanks, now how can i put that where in a for-loop ? with multiple values?
– Pc Monk
Nov 11 at 11:36
Need more info, what your loop is, to recommend best way
– Manpreet
Nov 11 at 11:54
an array of 3 tags from user , the loop makes 3 where clauses for me with those tags! thats the scenario.
– Pc Monk
Nov 11 at 12:03
1
laravel.com/docs/5.7/queries check for whereIn instead of where if you has array of multiple prof_ids
– Manpreet
Nov 11 at 12:15
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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();
Thanks, now how can i put that where in a for-loop ? with multiple values?
– Pc Monk
Nov 11 at 11:36
Need more info, what your loop is, to recommend best way
– Manpreet
Nov 11 at 11:54
an array of 3 tags from user , the loop makes 3 where clauses for me with those tags! thats the scenario.
– Pc Monk
Nov 11 at 12:03
1
laravel.com/docs/5.7/queries check for whereIn instead of where if you has array of multiple prof_ids
– Manpreet
Nov 11 at 12:15
add a comment |
up vote
1
down vote
accepted
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();
Thanks, now how can i put that where in a for-loop ? with multiple values?
– Pc Monk
Nov 11 at 11:36
Need more info, what your loop is, to recommend best way
– Manpreet
Nov 11 at 11:54
an array of 3 tags from user , the loop makes 3 where clauses for me with those tags! thats the scenario.
– Pc Monk
Nov 11 at 12:03
1
laravel.com/docs/5.7/queries check for whereIn instead of where if you has array of multiple prof_ids
– Manpreet
Nov 11 at 12:15
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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();
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();
answered Nov 11 at 11:25
Manpreet
40016
40016
Thanks, now how can i put that where in a for-loop ? with multiple values?
– Pc Monk
Nov 11 at 11:36
Need more info, what your loop is, to recommend best way
– Manpreet
Nov 11 at 11:54
an array of 3 tags from user , the loop makes 3 where clauses for me with those tags! thats the scenario.
– Pc Monk
Nov 11 at 12:03
1
laravel.com/docs/5.7/queries check for whereIn instead of where if you has array of multiple prof_ids
– Manpreet
Nov 11 at 12:15
add a comment |
Thanks, now how can i put that where in a for-loop ? with multiple values?
– Pc Monk
Nov 11 at 11:36
Need more info, what your loop is, to recommend best way
– Manpreet
Nov 11 at 11:54
an array of 3 tags from user , the loop makes 3 where clauses for me with those tags! thats the scenario.
– Pc Monk
Nov 11 at 12:03
1
laravel.com/docs/5.7/queries check for whereIn instead of where if you has array of multiple prof_ids
– Manpreet
Nov 11 at 12:15
Thanks, now how can i put that where in a for-loop ? with multiple values?
– Pc Monk
Nov 11 at 11:36
Thanks, now how can i put that where in a for-loop ? with multiple values?
– Pc Monk
Nov 11 at 11:36
Need more info, what your loop is, to recommend best way
– Manpreet
Nov 11 at 11:54
Need more info, what your loop is, to recommend best way
– Manpreet
Nov 11 at 11:54
an array of 3 tags from user , the loop makes 3 where clauses for me with those tags! thats the scenario.
– Pc Monk
Nov 11 at 12:03
an array of 3 tags from user , the loop makes 3 where clauses for me with those tags! thats the scenario.
– Pc Monk
Nov 11 at 12:03
1
1
laravel.com/docs/5.7/queries check for whereIn instead of where if you has array of multiple prof_ids
– Manpreet
Nov 11 at 12:15
laravel.com/docs/5.7/queries check for whereIn instead of where if you has array of multiple prof_ids
– Manpreet
Nov 11 at 12:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53248173%2fintegrity-constraint-violation-1052-column-prof-id-in-where-clause-is-ambiguo%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown