Andreas Hunter
Andreas Hunter

Reputation: 5004

Laravel: Query return wrong results

I have custom code to get vacancies:

Laravel Eloquent query:

$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 code:

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 is my table structure.

When I run this code or SQL query return vacancies with any employment. For example here in my code I need to vacancies with only employment = 0 but my current result return vacancies with any employments.

Where I have error in my code?

Upvotes: 3

Views: 370

Answers (1)

kapitan
kapitan

Reputation: 2212

Can you please try this:

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

Upvotes: 5

Related Questions