多个whereHas调用未聚合在一起

时间:2017-05-09 15:08:28

标签: laravel eloquent

我在whereHas()\Illuminate\Database\Query\Builder)的实例上多次调用$cars

$cars->whereHas("finance", function (Eloquent\Builder $query) {
    $query->where('term'...)
}
$cars->whereHas("finance", function (Eloquent\Builder $query) {
    $query->where('payment'...)
}

有没有办法将where(s)聚合在一起,而无需在包含where内进行所有whereHas次调用?

正在执行的SQL查询:

SELECT id
FROM `cars`
WHERE EXISTS
    (SELECT `finance`.`payment` as payment
    FROM `finance`
     INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id`
     WHERE `car_finance`.`car_id` = `cars`.`id`
       AND `payment` >= 50)
  AND EXISTS
    (SELECT *
     FROM `finance`
     INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id`
     WHERE `car_finance`.`car_id` = `cars`.`id`
       AND `payment` <= 200)
  AND EXISTS
    (SELECT *
     FROM `finance`
     INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id`
     WHERE `car_finance`.`car_id` = `cars`.`id`
       AND `term` = 48)
  AND EXISTS
    (SELECT *
     FROM `finance`
     INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id`
     WHERE `car_finance`.`car_id` = `cars`.`id`
       AND `deposit` = 1000)
  AND `active` = 1

我想要执行的SQL查询:

SELECT *
        FROM cars
WHERE EXISTS
    (SELECT *
        FROM `finance`
     INNER JOIN `car_finance` ON `finance`.`id` = `car_finance`.`finance_id`
     WHERE `car_finance`.`car_id` = `cars`.`id`
    AND deposit = 1000
    AND term = 48
    AND payment >= 50
    AND payment <= 200)
  AND active = 1

1 个答案:

答案 0 :(得分:0)

你的问题不清楚,但我认为你想要这个:

    <script>
//session end
    if (sessionTimer === undefined)
        var sessionTimer = 0;
    var sessionTimeoutWarning = @Session.Timeout- 1;
    var sTimeout = parseInt(sessionTimeoutWarning) * 60 * 1000;
    SetTimer();

    function SessionEnd() {
        document.getElementById('logoutForm').submit();
    }
    function SetTimer() {
        ClearTimer();
        sessionTimer = setTimeout('SessionEnd()', sTimeout);
    };
    function ClearTimer() {
        clearTimeout(sessionTimer);
    };
    $('body').on('click', function () {
            ClearTimer();
    });
</script>