我正在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;
});
目前我在该特定日期之间获得了投资数量的交互次数,我想获取某些日期字段之间的计数,即假设我想要连续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);
}
有没有办法通过收集来缩短这段代码?
答案 0 :(得分:0)
您可以尝试将列命名为不同的名称,
例如:
$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;
});
可能会有效。