2

I'm building a small application on Laravel 5.4 where I'm having data sets to are being fetched from the relationship something like this:

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

Currently I'm getting the number of interaction as investor_counts between that particular date, I want to fetch the counts between certain date fields, i.e. suppose I want to have the data to be fetched for consecutive 6 months, I mean data of 6 different months something like this:

->whereBetween('schedule', [Carbon::now()->subMonths(6), Carbon::now()->subMonths(5)])

and

->whereBetween('schedule', [Carbon::now()->subMonths(5), Carbon::now()->subMonths(4)])

and

->whereBetween('schedule', [Carbon::now()->subMonths(4), Carbon::now()->subMonths(3)])

And the list goes on similar to above differences. And I don't want to implement multiple foreach loops, is there any way out with Laravel Collection where I can manipulate such data the get the output. Please guide me. Thanks.

Edit: Let me explain in better way, currently I'm using this code to get my desired data:

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

Is there any way to shorten this code through collection?

Nitish Kumar
  • 6,054
  • 21
  • 82
  • 148
  • you can directly fetch records between from 1st date and the last date and then order by date – Rohit shah Nov 30 '17 at 07:18
  • 1
    @Rohitshah I think you didn't get my question. I'm able to fetch records between two dates, I want to fetch records for each months i.e. between current date and last month, between last month to previous 2 months, like this. – Nitish Kumar Nov 30 '17 at 07:24
  • Did you work out how best to achieve this in the end? – ejntaylor Jul 18 '19 at 10:02

1 Answers1

0

you may try to give column as different name,

For example :

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

May be this will work.

Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
  • Hi Bhavin thanks for the update, but I'm counting the list of interaction as investors_count between those dates and I want to have those counts for separate dates, sorry if I was not clear in the question. I've update the question too – Nitish Kumar Nov 30 '17 at 07:32