我如何一次只能忽略当前行ID

时间:2018-12-08 07:26:14

标签: laravel eloquent laravel-5.7

我有自定义查询,其中忽略了当前行ID:

$relatedVacancies = \App\Vacancy::whereHas('skills', function ($q) use ($vacancy) {
    return $q->whereIn('skill_id', $vacancy->skills->pluck('skill_id')); 
  })->where('id', '!=', $vacancy->id)
  ->orWhere('employment', $vacancy->employment)->where('id', '!=', $vacancy->id)
  ->orWhere('title', 'like', '%'.$vacancy->title.'%')->where('id', '!=', $vacancy->id)
->get();

如何只能一次使用此条件:

->where('id', '!=', $vacancy->id)

2 个答案:

答案 0 :(得分:0)

如果您尝试基于以下三个条件进行选择:

->where([
  ('id', '!=', $vacancy->id),
  ('employment', '=', $vacancy->employment),
  ('title', 'like', '%'.$vacancy->title.'%'),
])->get();

答案 1 :(得分:0)

尝试一下(基于https://laravel.com/docs/5.7/queries#parameter-grouping):

$relatedVacancies = \App\Vacancy::where('id', '!=', $vacancy->id)
         ->whereHas('skills', function ($q) use ($vacancy) {
             return $q->whereIn('skill_id', $vacancy->skills->pluck('skill_id')); 
         })
         ->where(function ($query) {
            $query->where('employment', $vacancy->employment)
                  ->orWhere('title', 'like', '%'.$vacancy->title.'%');
        })
        ->get();

还要检查以下答案:https://stackoverflow.com/a/23009807/5458355