我正在使用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');

最佳答案

将外键和本地键放到关系中。

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
}

关于php - “where”子句中的未知列“has”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58542395/

10-12 13:15