我正在构建一个小型应用程序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;
});
目前我正在获取特定日期之间的交互次数作为investor_counts,我想获取某些日期字段之间的计数,即假设我希望连续6个月获取数据,我的意思是6个不同月份的数据像这样的东西:
->whereBetween('schedule', [Carbon::now()->subMonths(6), Carbon::now()->subMonths(5)])
和
->whereBetween('schedule', [Carbon::now()->subMonths(5), Carbon::now()->subMonths(4)])
和
->whereBetween('schedule', [Carbon::now()->subMonths(4), Carbon::now()->subMonths(3)])
该列表与上述差异类似。而且我不想实现多个 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);
}
有没有办法通过收集来缩短这段代码?