0

Задача (бронирование):

есть поля data, date_from, date_to

необходимо в запрос передать дату '2022.09.15' и вывести все записи, где date <= '2022.09.15' и '2022.09.15' не находится в промежутке между date_from и date_to

Есть такой запрос, который нормально отрабатывает:

select
    *
from
    positions p
    left join reserved_positions rp on p.id = rp.position_id
where
    p.date <= '2022.09.15'
    and '2022.09.15' not between rp.date_from and rp.date_to

Как его правильно переделать в laravel?

2 ответа 2

0

Ну вроде здесь всё просто. Но для начала организовываете связи многие ко многим через модели. И после делаете следующее:

Position::where('date', '>=', '2022.09.15')
    ->whereHas('reserves', function($q) {
        $q->where('reserved_positions.date_from', '<=', '2022.09.15');
        $q->where('reserved_positions.date_to', '>=', '2022.09.15');
    })
    ->get();
0
0

Как вариант, вот так попробовать...

Position::where('date', '>=', '2022.09.15')
    ->leftJoin('reserved_positions', 'positions.id', '=', 'reserved_positions.position_id')
    ->where('positions.date', '<=', '2022.09.15')
    ->whereBetween('2022.09.15', ['reserved_positions.date_from', 'reserved_positions.date_to'])
    ->get();

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.