我正在使用laravel 5.7
当我使用whereHas
检查关系的存在时,我遇到Unknown column "has" in 'where clause
错误!为什么?
这是我的代码:
$query->whereHas('departures', function ( $query) {
$query->whereBetween('depart_date', [date($this->data['departFrom_date']), date($this->data['departTo_date'])]);
});
这是我的模型部分:
public function departures()
{
return $this->hasMany(CruiseSailingDepartures::class);
}
更新:
查询始于:
$query = DB::table('table_name');
答案 0 :(得分:3)
将外键和本地键放在关系中。
public function departures()
{
return $this->hasMany('App\Model\CruiseSailingDepartures','foreign_key','local_key');
}
$res = BaseModel::find($id);
foreach($res->departures as $departures)
{
echo $departures->name; //whatever field you have in CruiseSailingDepartures table
}
答案 1 :(得分:0)
$model = YourModel::with('departures')->whereHas('departures', function($query) use ($this) {
$query->whereBetween('depart_date', [date($this->data['departFrom_date']), date($this->data['departTo_date'])]);
})->get();
答案 2 :(得分:0)
我发现了问题,我在使用DB::table('tablename')
而不是模型(TableName:: ...
)
感谢所有答案