为什么我的whereHas使用未定义的方法查询错误" getHasCompareKey()"?

时间:2017-11-21 21:22:18

标签: php mongodb laravel eloquent

我有一个模型Job,它附加到一个模型Project

以下是类定义:

class Job extends Model
{
    public function project() {
        return $this->belongsTo('App\Project');
    }
}

class Project extends Model
{
    public function jobs() {
        return $this->hasMany('App\Job');
    }
}

我尝试查询Jobs集合并过滤jobs.titleproject.title

这是我当前的搜索查询:

$jobs = Job::where(function($query) use ($searchTerm) {
            $query->where('title', 'LIKE', $searchTerm)
                    ->orWhereHas('project', function ($subQuery) use ($searchTerm) {
                            return $subQuery->where('title', 'LIKE', $searchTerm);
                    });
        })->get();

但是,这会返回错误:

Call to undefined method Jenssegers\Mongodb\Query\Builder::getHasCompareKey()

1 个答案:

答案 0 :(得分:2)

我认为您需要稍微清理一下代码:

$jobs = Job::where('title', 'LIKE', $searchTerm)
            ->orWhereHas('project', function ($query) use ($searchTerm) {
                            $query->where('title', 'LIKE', $searchTerm);
                        })->get();