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 '%26@qq.com%' )
   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

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值