【问题标题】:How to get the row with two or more specific condition?如何获得具有两个或多个特定条件的行?
【发布时间】:2019-02-20 05:59:01
【问题描述】:

ApplicantTbl

id | name          |
--------------------
 1 | John Doe      |
 2 | Maria Ozawa   |
 3 | Catriona Gray |
--------------------

EnrollmentRequestContentTbl

id |  applicant_id | payment_status  |   enrollment_status   |
--------------------------------------------------------------
 1 |       1       |     pending     |        null           |
--------------------------------------------------------------

我的目标是获取所有的应聘者,包括申请人id 1 if

  1. enrollmentRequestContentTbl 上的 payment_status付费
  2. enrollmentRequestContentTbl 上的payment_status未付费enrollment_statusback_outdeclined

其余可能的条件将被忽略,并且无法从我的查询中获取。

上表将忽略申请人 1,因为 payment_statuspending

如果 EnrollmentRequestContentTbl 看起来像这样

 id |  applicant_id | payment_status  |   enrollment_status   |
 --------------------------------------------------------------
 1 |       1        |     paid        |        null           |
 --------------------------------------------------------------

或者这个。由于paymet_status 未支付 enrollment_status 必须back_out拒绝

 id |  applicant_id | payment_status  |   enrollment_status   |
 --------------------------------------------------------------
 1 |       1        |     unpaid      |        back_out       |
 --------------------------------------------------------------

-

 id |  applicant_id | payment_status  |   enrollment_status   |
 --------------------------------------------------------------
 1 |       1        |     unpaid      |        declined       |
 --------------------------------------------------------------

申请人 1 将包含在我的查询中并显示在我的列表中。

- ApplicantModel

public function enrollmentRequestContent()
{
   return $this->hasMany('App\EnrollmentRequestContent');
}

EnrollmentRequestContentModel

public function applicant()
{
   return $this->belongsTo('App\Applicant')
}

到目前为止,这是我尝试过的。

控制器

public function getApplicant()
{
    $applicants = Applicant::orWhereDoesntHave('enrollmentRequestContent', function($q){
        $q->where('payment_status', '!=', 'paid')
            ->where(function($qq) {
                $qq->where('payment_status', '!=', 'unpaid')
                        ->whereIn('enrollment_status', ['declined', 'back_out']);
            });
    })->get();   

    return $applicants;
}

我正在使用 laravel 雄辩的查询。

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    你实际上很接近。你只需要改变逻辑,这就是我所做的:

    首先,使用whereDoesntHave获取pivot table中不存在的所有申请人。

    接下来是获取pivot table 中存在的所有申请人仅当其状态为“已付款”(通过使用orWhereHas其状态为“未付款” ' 但其注册状态应该是 'back_out' 或 'declined' (通过使用 orWhereHaswhereIn)。

    $data = Applicant::whereDoesntHave('enrollmentRequestContents')
        ->orWhereHas('enrollmentRequestContents', function($q1){
            $q1->where('payment_status', 'paid');
        })
        ->orWhereHas('enrollmentRequestContents', function($q2){
            $q2->where('payment_status', 'unpaid')
               ->whereIn('enrollment_status', ['back_out', 'declined']);
        })
        ->get(); 
    

    【讨论】:

      【解决方案2】:

      你可以试试下面的代码会满足你的条件。

      public function getApplicant()
      {
            $applicants = Applicant::with(['enrollmentRequestContent'])
                      ->whereHas('enrollmentRequestContent', function($query){
                          $query->where(function($q){
                              $q->whereIn('payment_status', ['declined', 'back_out'])
                               ->where('payment_status', '!=','unpaid');
                          });
                          $query->orWhere('payment_status', '!=', 'paid');
                      })
      
           return $applicants;
      }
      

      【讨论】:

      • 我将payment_status 更改为pending 并且必须不属于该查询。但在你的回答中它显示了。另外,我的目标是让所有申请人都进入 applicanttbl 但在您的回答中它只显示具有 enrollmentRequestContent. 的应用程序
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 2021-03-21
      • 2016-09-30
      • 2017-09-21
      • 1970-01-01
      • 2020-09-05
      • 1970-01-01
      相关资源
      最近更新 更多