简体   繁体   English

Laravel 5.4 SQL查询嵌套在何处/何处

[英]Laravel 5.4 SQL Query Nested Where/OrWhere

I have these tables 我有这些桌子

Loans Table | 贷款表 | coop_id | coop_id | loan_id | 贷款编号| loan_availability | 贷款可用性|

Loan Seasons Table | 贷款季节表 | coop_id | coop_id | loan_id | 贷款编号| loan_season_start | loan_season_start | loan_season_end | 贷款季节结束|

And I'm trying to get the details of each loan in one array whether their availability is Always or Seasonal . 而且我正在尝试以阵列的形式获取每笔贷款的详细信息,无论其可用性为Always还是Seasonal If it is marked as seasonal, it has a data in the loan seasons table. 如果标记为季节性,则在“贷款季节”表中有数据。

This is my query 这是我的查询

$loanlist = DB::table('loans')
        ->join('loan_seasons', 'loan_seasons.loan_id', 'loans.loan_id')
        ->where('loans.coop_id', $coop_id)
        ->where(function ($q){ 
            $q->where('loans.loan_availability', "Always")
            ->orWhere(function ($qq){ 
                $qq->where('loans.loan_availability', "Seasonal")  
                ->where('loan_season_start', '>=', Carbon::now()->month)
                ->where('loan_season_end', '<=', Carbon::now()->month)
                ->where('loan_season_status', 1);
            });
        })
        ->where('loans.loan_status', "1")
        ->get()->toArray();

I'm getting this output: 我得到以下输出:

Array(
    [0] => Array(
        [id] => 2
        [coop_id] => 1
        [loan_id] => 3
        [loan_name] => Loan 1
        [loan_desc] => Loan 1 Description
        [loan_maxamount] => 500000
        [loan_availability] => Always
        [loan_status] => 1
        [created_at] => 2017-02-26 17:43:08
        [updated_at] => 2017-02-26 17:43:08
        [loan_season_start] => 2
        [loan_season_end] => 6
        [loan_season_status] => 1
    )
)

My expected output is like this: 我的预期输出是这样的:

Array(
    [0] => Array(
        [id] => 1
        [coop_id] => 1
        [loan_id] => 3
        [loan_name] => Loan 1
        [loan_desc] => Loan 1 Description
        [loan_maxamount] => 500000
        [loan_availability] => Always
        [loan_status] => 1
        [created_at] => 2017-02-26 17:43:08
        [updated_at] => 2017-02-26 17:43:08
        [loan_season_status] => 1
    )
   [1] => Array(
        [id] => 2
        [coop_id] => 1
        [loan_id] => 4
        [loan_name] => Loan 2
        [loan_desc] => Loan 2 Description
        [loan_maxamount] => 200000
        [loan_availability] => Seasonal
        [loan_season_start] => 2
        [loan_season_end] => 6
        [loan_status] => 1
        [created_at] => 2017-02-26 17:43:08
        [updated_at] => 2017-02-26 17:43:08
        [loan_season_status] => 1
    )
)

I've been trying to solve this for hours but I still can't get the query right. 我已经尝试解决了几个小时,但仍然无法正确查询。 Can somebody help? 有人可以帮忙吗? I'm very new to nested queries. 我是嵌套查询的新手。 Help? 救命?

I got it working now. 我现在开始工作了。 Seems like I've been making simple queries complicated. 好像我一直在使简单的查询变得复杂。 I created different queries for each condition and joined them into one instead. 我为每种条件创建了不同的查询,并将它们合并为一个查询。 Thanks. 谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM