繁体   English   中英

如何在Laravel中获取多个whereBetween记录

[英]How to get multiple whereBetween records in Laravel

我在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,我想获取某些日期字段之间的计数,即假设我想连续六个月获取数据,我的意思是六个不同月的数据像这样的东西:

->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);
}

有什么办法可以通过收集来缩短这段代码?

您可以尝试为列指定其他名称,

例如 :

$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;
    });

也许这会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM