0

Hi, I am trying to get Laravel group array data total can anyone help me ?

controller code

public function last_10_days_reports()
    {
        $day_10 = Carbon::today()->subDays(10)->format('Y-m-d');
        $leads = Lead::whereDate('created_at', '>=', $day_10)
            ->get()
            ->sortBy('created_at')
            ->groupBy(function ($item) {
                return $item->created_at->format('Y-m-d');
            })->toArray();

        return $leads;
    }

I get like below screenshot data but I want to total array number. can anyone help me ?

http://prntscr.com/159d9fu

8
  • $leads = Lead::whereDate('created_at', '>=', $day_10)->groupBy('created_at') ->get() what about this Commented Jun 13, 2021 at 12:10
  • Do you need result, like ['2021-06-12' => 5, '2021-06-11' => 4, ...] ? Commented Jun 13, 2021 at 12:14
  • @JohnLobo getting error prntscr.com/159f4bo Commented Jun 13, 2021 at 12:20
  • @AndrewMarkhai Exactly I want Commented Jun 13, 2021 at 12:20
  • try 'strict' => false, in database.php for mysql Commented Jun 13, 2021 at 12:23

1 Answer 1

2

Try this:

Lead::query()
            ->selectRaw('date(created_at) as date, count(*) as count')
            ->where('created_at', '>=', $day_10)
            ->orderByRaw('date(created_at)')
            ->groupByRaw('date(created_at)')
            ->get()
            ->pluck('count', 'date')
            ->toArray();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.