Answer the question
In order to leave comments, you need to log in
How can I create such a request?
I have a task: to select inactive users who have a work (works) that has been closed (works_closed), but so that a new work is not opened from the moment he closed the last one, since then the user is already considered active. An active job is considered one that has roles and start_date starts before the current date.
I have this code in Laravel, but it selects everyone who has a closed job, not taking into account that a new one may be open. If you don't know how to do it in Laravel I'll be grateful even for pure SQL :)
User::whereHas('works', function ($query) {
$query->whereHas('works_closed', function ($query) {
$query->where('closed_at', '<=', now());
});
})
->get();
- id
- name
- id
- user_id
- name
- id
- work_id
- closed_at
- id
- name
- start_date
- end_date
Answer the question
In order to leave comments, you need to log in
User::query()
->whereHas('works', function ($query) {
$query->whereHas('works_closed', function ($query) {
$query->where('closed_at', '<=', now());
});
})
->whereDoesntHave('works', function ($query) {
$query->where('roles', '!=', null);
$query->where('start_date', '>', now());
})
->get();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question