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!










share|improve this question



























    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!










    share|improve this question

























      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!










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 12 at 4:50









      Takamura

      911212




      911212










      asked Nov 11 at 11:16









      Pc Monk

      5619




      5619






















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





          share|improve this answer




















          • 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










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          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

























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





          share|improve this answer




















          • 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














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





          share|improve this answer




















          • 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












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





          share|improve this answer












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






          share|improve this answer












          share|improve this answer



          share|improve this answer










          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
















          • 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

















          draft saved

          draft discarded
















































          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.




          draft saved


          draft discarded














          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





















































          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







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          Node.js Script on GitHub Pages or Amazon S3

          Museum of Modern and Contemporary Art of Trento and Rovereto