Laravel:查询返回错误结果

时间:2019-04-03 05:38:45

标签: mysql sql laravel eloquent

我有自定义代码以获取职位空缺:

Laravel雄辩的查询:

$vacancy = new Vacancy;
$vacancy = $vacancy->has('user')->orWhere('is_express', 1);
$where = [];
$date = Carbon::now()->subDays(10);
$where[] = ['deleted_at', null];
$where[] = ['updated_at', '>=', $date];
$employment = [0];
$vacancy = $vacancy->whereIn('employment', $employment);
$vacancy = $vacancy->where($where)->get();

SQL代码:

select * from `vacancies`
where(exists(select * from `users`
    where `vacancies`.
    `user_id` = `users`.
    `id`
    and `users`.
    `deleted_at`
    is null) or `is_express` = 1 and `employment` in ( 0 ) and(`deleted_at`
    is null and `updated_at` >= '2019-03-22 08:48:34' )) and `vacancies`.
`deleted_at`
is null

Here是我的表结构。

当我运行此代码或SQL查询时,返回带有任何employment的空缺。例如,在我的代码中,我只需要employment = 0的职位空缺,但是我当前的结果返回任何职位的职位空缺。

我的代码有错误吗?

1 个答案:

答案 0 :(得分:5)

您可以尝试一下吗?

$vacancy = Vacancy::whereNull('deleted_at')->where('updated_at', '>=', $date)
  ->whereIn('employment', $employment)
  ->where(function($query) {
    return $query->has('user')->orWhere('is_express', 1);
});