【问题标题】:Laravel 4.1 whereHas [ Integrity constraint violation: 1052 Column 'confirm' in where clause is ambiguous ]Laravel 4.1 whereHas [完整性约束违规:1052列'确认'在where子句不明确]
【发布时间】: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-&gt;where('teams.confirm',1);course_team.confirm 左右。 (虽然,我可能是错的)
  • 感谢 Jari,我使用了我的数据透视模型 course_team() 方法 "$q->where('course_team.confirm',1);"它奏效了。
  • 很高兴为您提供帮助 :) 我发布了我的答案,请随时接受。 (打勾)

标签: laravel-4 eloquent pivot-table


【解决方案1】:

好吧,只是作为答案:您应该指定与confirm 相关的表。

$q->where('course_team.confirm',1);

【讨论】:

    猜你喜欢
    • 2020-10-13
    • 2019-04-14
    • 2020-11-23
    • 2013-10-16
    • 2022-08-18
    • 1970-01-01
    • 2019-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多