我在使用whereHas时遇到问题,这是代码:
<?php
$courses = Course::whereHas('teams', function($q)
{
$q->where('confirm',1);
})->get();
// $courses = Course::has('teams')->get();
?>
错误:
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);
}
我想要做的是获得他们为团队分配的课程 在数据透视表中:&#39; course_team&#39;确认&#39;列是真的&#39;
has()方法,用于获得团队工作正常的课程,
我该如何运作?
答案 0 :(得分:1)
好吧,只是为了作为答案:你应该指定与confirm
相关的表格。
$q->where('course_team.confirm',1);