2

I'm having trouble using whereHas, here is the code:

<?php    
$courses = Course::whereHas('teams', function($q)    
{
$q->where('confirm',1);                 
})->get();      
// $courses = Course::has('teams')->get();
?>

Error:

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'confirm' in where clause                is ambiguous (SQL: select * from `courses` where (select count(*) from `teams` inner join  `course_team` on `teams`.`id` = `course_team`.`team_id` where `course_team`.`course_id` =  `courses`.`id` and `confirm` = 1) >= 1) (View:  /Applications/MAMP/htdocs/learnvenue/app/views/dashboard/trainer/index.blade.php)

open: /Applications/MAMP/htdocs/learnvenue/vendor/laravel/framework/src/Illuminate/Database/Connection.php
    }

    // If an exception occurs when attempting to run a query, we'll format the error
    // message to include the bindings with SQL, which will make this exception a
    // lot more helpful to the developer instead of just the database's errors.
    catch (\Exception $e)
    {
        throw new QueryException($query, $bindings, $e);
    }

what i want to do is that to get courses which they have teams assigned to them where in pivot table:'course_team' the 'confirm' column is 'true'

has() method for just getting courses that have teams working fine,

How do I get this working?

Xolmer
  • 375
  • 1
  • 4
  • 6
  • The error can be happening if a column occurs in several tables of pseudo tables when you `join` them. My guess is to use `$q->where('teams.confirm',1);` or `course_team.confirm` or so. (Though, I can be wrong) – sybear Mar 13 '14 at 17:52
  • Thanks Jari i used my pivot model course_team() method "$q->where('course_team.confirm',1);" and it worked. – Xolmer Mar 13 '14 at 17:58
  • Glad to help :) I posted my answer, so feel free to accept it. (Put a tick) – sybear Mar 13 '14 at 18:05

1 Answers1

2

Well, just to make it as an answer: you should specify the table to which confirm is related.

$q->where('course_team.confirm',1);
sybear
  • 7,837
  • 1
  • 22
  • 38