【问题标题】:Laravel relationship Column 'id' in where clause is ambiguousLaravel关系列'id'在where子句中不明确
【发布时间】:2017-07-22 16:26:17
【问题描述】:
我有课程和订阅类型。
我想获取所有具有给定订阅类型的课程。
我的尝试:
$courses=Course::wherehas('subscriptionType',function ($q)
{
return $q->where('id','1');
})->get();
但这失败了:
where 子句中的列 'id' 不明确
有什么建议吗?
【问题讨论】:
标签:
php
laravel
laravel-5
eloquent-relationship
mysql-error-1052
【解决方案1】:
我测试了您的代码,它运行良好,没有任何更改。您的关系定义可能有问题。
但是,您可以通过进行以下更改使其运行。
将 return $q->where('id','1'); 替换为 return $q->where('subscriptiontypes.id','1'); 我假设订阅类型模型的表名是订阅类型。
完整代码如下:
$courses=Course::wherehas('subscriptiontype',function ($q)
{
return $q->where('subscriptiontypes.id','1');
})->get();