Php 如何在Laravel中获取多个whereween记录

Php 如何在Laravel中获取多个whereween记录,php,laravel,laravel-5.4,laravel-eloquent,laravel-collection,Php,Laravel,Laravel 5.4,Laravel Eloquent,Laravel Collection,我正在Laravel 5.4上构建一个小应用程序,在这里我要从关系中获取数据集,如下所示: $meetings = Company::where('name', 'Atlanta Ltd') ->withCount(['interactions as investors'=> function ($q) { $q ->whereBetween('schedule', [Carbon::now()->subMonths(6),

我正在
Laravel 5.4
上构建一个小应用程序,在这里我要从关系中获取数据集,如下所示:

$meetings = Company::where('name', 'Atlanta Ltd')
    ->withCount(['interactions as investors'=> function ($q) {
        $q
            ->whereBetween('schedule', [Carbon::now()->subMonths(6), Carbon::now()->subMonths(1)])
            ->whereHas('contactsAssociation', function ($q) {
                $q->whereHas('company', function ($q) {
                    $q->where('type', 'like', 'Investor');
                });
            });
    }])->get()
    ->transform(function ($company) {
        $company->investors_count = $company->interactions
            ->pluck('investors_count')
            ->sum();
        return $company;
    });
->whereBetween('schedule', [Carbon::now()->subMonths(6), Carbon::now()->subMonths(5)])
目前,我得到的是在特定日期之间的交互次数,我想获取特定日期字段之间的次数,即,假设我想获取连续6个月的数据,我指的是6个不同月份的数据,如下所示:

$meetings = Company::where('name', 'Atlanta Ltd')
    ->withCount(['interactions as investors'=> function ($q) {
        $q
            ->whereBetween('schedule', [Carbon::now()->subMonths(6), Carbon::now()->subMonths(1)])
            ->whereHas('contactsAssociation', function ($q) {
                $q->whereHas('company', function ($q) {
                    $q->where('type', 'like', 'Investor');
                });
            });
    }])->get()
    ->transform(function ($company) {
        $company->investors_count = $company->interactions
            ->pluck('investors_count')
            ->sum();
        return $company;
    });
->whereBetween('schedule', [Carbon::now()->subMonths(6), Carbon::now()->subMonths(5)])

这个列表与上面的差异类似。我不想实现多个foreach循环,有没有办法使用
Laravel Collection
来处理这些数据并获得输出。请引导我。谢谢

编辑: 让我以更好的方式解释一下,目前我正在使用此代码获取所需的数据:

public function investorGraphData(Request $request)
{
    $weekInvestorData = $weekResearchData = [];

    if($request->timeFormat == 'month')
    {
        for($i=0; $i<6; $i++)
        {
            $meetings = Company::where('name', $request->client)
                ->withCount(['interactions as investors'=> function ($q) use($i) {
                    $q
                        ->whereBetween('schedule', [Carbon::now()->subMonth($i+1), Carbon::now()->subMonth($i)])
                        ->whereHas('contactsAssociation', function ($q) {
                            $q->whereHas('company', function ($q) {
                                $q->where('type', 'like', 'Investor');
                            });
                        });
                }])
                ->withCount(['interactions as research' => function($q) use($i) {
                    $q
                        ->whereBetween('schedule', [Carbon::now()->subMonth($i+1), Carbon::now()->subMonth($i)])
                        ->whereHas('contactsAssociation', function ($q) {
                            $q->whereHas('company', function ($q) {
                                $q->where('type', 'like', 'Research');
                            });
                        });
                }])
                ->get();
            $weekInvestorData[] = $meetings[0]->investors_count;
            $weekResearchData[] = $meetings[0]->research_count;
        }

        return response()->json(['investor' => $weekInvestorData, 'research' => $weekResearchData], 200);
    }
    elseif ($request->timeFormat == 'week')
    {
        for($i=0; $i<6; $i++)
        {
            $meetings = Company::where('name', $request->client)
                ->withCount(['interactions as investors'=> function ($q) use($i) {
                    $q
                        ->whereBetween('schedule', [Carbon::now()->subWeek($i+1), Carbon::now()->subWeek($i)])
                        ->whereHas('contactsAssociation', function ($q) {
                            $q->whereHas('company', function ($q) {
                                $q->where('type', 'like', 'Investor');
                            });
                        });
                }])
                ->withCount(['interactions as research' => function($q) use($i) {
                    $q
                        ->whereBetween('schedule', [Carbon::now()->subWeek($i+1), Carbon::now()->subWeek($i)])
                        ->whereHas('contactsAssociation', function ($q) {
                            $q->whereHas('company', function ($q) {
                                $q->where('type', 'like', 'Research');
                            });
                        });
                }])
                ->get();
            $weekInvestorData[] = $meetings[0]->investors_count;
            $weekResearchData[] = $meetings[0]->research_count;
        }
        return response()->json(['investor' => $weekInvestorData, 'research' => $weekResearchData], 200);
    }
    else
        return response()->json(['message' => 'No input given'], 200);
}
公共功能investorGraphData(请求$Request)
{
$weekInvestorData=$weekResearchData=[];
如果($request->timeFormat=='month')
{
对于($i=0;$iclient)
->withCount(['interactions as investors'=>函数($q)使用($i){
$q
->其中('schedule',[Carbon::now()->子月($i+1),Carbon::now()->子月($i)])
->其中有('contactsAssociation',功能($q){
$q->whereHas('company',function($q){
$q->where('type','like','Investor');
});
});
}])
->withCount(['interactions as research'=>函数($q)使用($i){
$q
->其中('schedule',[Carbon::now()->子月($i+1),Carbon::now()->子月($i)])
->其中有('contactsAssociation',功能($q){
$q->whereHas('company',function($q){
$q->where('type','like','Research');
});
});
}])
->get();
$weekInvestorData[]=$meetings[0]->投资者计数;
$weekResearchData[]=$meetings[0]->研究计数;
}
return response()->json(['investor'=>$weekInvestorData,'research'=>$weekResearchData],200);
}
elseif($request->timeFormat=='week')
{
对于($i=0;$iclient)
->withCount(['interactions as investors'=>函数($q)使用($i){
$q
->其中('schedule',[Carbon::now()->子周($i+1),Carbon::now()->子周($i)])
->其中有('contactsAssociation',功能($q){
$q->whereHas('company',function($q){
$q->where('type','like','Investor');
});
});
}])
->withCount(['interactions as research'=>函数($q)使用($i){
$q
->其中('schedule',[Carbon::now()->子周($i+1),Carbon::now()->子周($i)])
->其中有('contactsAssociation',功能($q){
$q->whereHas('company',function($q){
$q->where('type','like','Research');
});
});
}])
->get();
$weekInvestorData[]=$meetings[0]->投资者计数;
$weekResearchData[]=$meetings[0]->研究计数;
}
return response()->json(['investor'=>$weekInvestorData,'research'=>$weekResearchData],200);
}
其他的
return response()->json(['message'=>'没有输入'],200);
}

有没有办法通过集合来缩短此代码?

您可以尝试为列指定不同的名称

例如:

$meetings = Company::where('name', 'Atlanta Ltd')
    ->withCount(['interactions as investors'=> function ($q) {
        $q
            ->whereBetween('schedule as schedule6to5', [Carbon::now()->subMonths(6), Carbon::now()->subMonths(5)])
            ->whereBetween('schedule as schedule5to4', [Carbon::now()->subMonths(5), Carbon::now()->subMonths(4)])
            ->whereBetween('schedule as schedule4to3', [Carbon::now()->subMonths(4), Carbon::now()->subMonths(3)])
            ->whereHas('contactsAssociation', function ($q) {
                $q->whereHas('company', function ($q) {
                    $q->where('type', 'like', 'Investor');
                });
            });
    }])->get()
    ->transform(function ($company) {
        $company->investors_count = $company->interactions
            ->pluck('investors_count')
            ->sum();
        return $company;
    });

这可能有效。

您可以直接获取从第一个日期到最后一个日期之间的记录,然后按date@Rohitshah我想你没有明白我的问题。我可以在两个日期之间提取记录,我想在每个月提取记录,即当前日期和上个月之间,上个月和前两个月之间,如下所示。您是否找到了最终实现这一目标的最佳方法?嗨,Bhavin,谢谢您的更新,但我正在计算这些日期之间的互动列表,我想将这些计数用于单独的日期,抱歉,如果我不清楚这个问题。我也更新了这个问题