【发布时间】:2014-03-13 17:49:06
【问题描述】:
我在使用 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);
}
我想做的是获得他们分配给他们的团队的课程 在数据透视表中:“course_team”中的“确认”列是“真”
has() 方法用于获取团队工作正常的课程,
我如何让它工作?
【问题讨论】:
-
当您
join它们时,如果一个列出现在多个伪表中,则可能会发生错误。我的猜测是使用$q->where('teams.confirm',1);或course_team.confirm左右。 (虽然,我可能是错的) -
感谢 Jari,我使用了我的数据透视模型 course_team() 方法 "$q->where('course_team.confirm',1);"它奏效了。
-
很高兴为您提供帮助 :) 我发布了我的答案,请随时接受。 (打勾)
标签: laravel-4 eloquent pivot-table