Laravel多表連接,多個查詢(Eloquent)

1.(寫此文章理由) 最近工作用laravel ,然而我不會laravel,工作快四年了,初次使用compose,初次使用git,學習laravel。工作這次做一個查詢,如圖下,本來下SQL的話很快,但是用Eloquent

2.要寫的sql ,用Eloquent

select crm_prospect_profiles.phone_mobile,
  crm_prospect_profiles.email ,
  crm_tasks.name,
  crm_prospect_childs.birthdate,
  dm_courses.title
from crm_prospects 
left join crm_prospect_childs on crm_prospects.child_id=crm_prospect_childs.id
left join crm_tasks on crm_tasks.assigned_user_id=crm_prospects.assigned_user_id
left join crm_prospect_profiles on crm_prospect_profiles.id=crm_prospects.profile_id
left join enrolled_students on enrolled_students.student_id=crm_prospect_childs.id
left join dm_associated_courses on dm_associated_courses.id=enrolled_students.associated_course_id
left join dm_courses on dm_courses.id=dm_associated_courses.course_id
where 
    (crm_prospect_profiles.phone_mobile like '%157%' 
   or crm_prospect_profiles.email like '%[email protected]%' )
   and dm_courses.id=''
   and crm_prospect_childs.birth>$bigenDate
   and crm_prospect_childs.birth<$endDate
   and crm_tasks.name=''

這個地方把我難住了,在主模型,關聯太多從模型,而且從模型的字段做查詢篩選

(模型關係怎麼寫這裏不說了哈)

一 初步 :實現主表連接從表,從表做查詢篩選

$crm_prospects = CrmProspect::whereHas('profile',
    function ($q) use ($keyword)
    {
        $q->where('phone_mobile', 'like', '%' . $keyword . '%');
        $q->orWhere('email', 'like', '%' . $keyword . '%');
    })->get();

先掌握這個再看下面的(文檔中可以學)。

但是呢,從表連表 再連表呢,媽蛋,怎麼玩

二 進入問題難點

 我的處理方法是 在function中寫function,嵌套function,最後好了(我只能說,我不知道其他方法了)。

上代碼

public function searchCrmProspects($age,$birStartDate,$birEndDate,$course_id,$lastAction,$keyword) {
    $crm_prospects = CrmProspect::whereHas('profile',
        function ($q) use ($keyword)
        {
            $q->where('phone_mobile', 'like', '%' . $keyword . '%');
            $q->orWhere('email', 'like', '%' . $keyword . '%');
        });

    if(!empty($age)){
        $crm_prospects->wherehas('child',
            function ($q) use ($birStartDate,$birEndDate)
            {
                $q->where('birthdate', '>',$birStartDate);
                $q->where('birthdate', '<=',$birEndDate);

            });
    }

    if(!empty($course_id)){
        $crm_prospects->wherehas('child',
            function ($q) use ($course_id)
            {
                $q->wherehas('enrolledStudents',
                    function ($q) use ($course_id){
                        $q->wherehas('associatedCourse',
                            function ($q) use ($course_id){
                                $q->wherehas('dmCourse',
                                    function ($q) use ($course_id){
                                        $q->where('id', '=',$course_id);
                                    }
                                );
                            }
                        );
                    }
                );
            });
    }

    if (!empty($lastAction)){
        $crm_prospects->wherehas('tasks',
            function ($q) use ($lastAction)
            {
                $q->where('name', '=', $lastAction);
            });
    }

    return $crm_prospects->get();
}

有問題加 QQ:269074092

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章