0

$gno =head::select('point')->where('DATE', '>', Carbon::now()->subDays(7))-> orderBy('DATE','desc')->get();

return $gno;

Above code returns past seven days record from point column and i counted this using Below code

 $gcs=$gno->count();
  $gccount=explode(', ', $gcs);
  return $gccount;

Its returns ["36"] count as seven days Records but I Expected : I want To store each date record count as array Example like ["5","15","20","12","4","2","4"] count as starting from today i think this will done by use loops But i dont know hoe to fix it??Thanks

5
  • Do you want the data date-wise? Right? Commented Jun 18, 2021 at 10:55
  • yes from today to past days Commented Jun 18, 2021 at 10:57
  • Then simply group by date and count. Commented Jun 18, 2021 at 10:58
  • I dont know how to do? Commented Jun 18, 2021 at 11:00
  • check the answer. Commented Jun 18, 2021 at 11:07

2 Answers 2

2

I think you have to read the Laravel collection methods which help here to get the data date-wise.

We first get the basic data with the order by clause.

Then the database collection return by query, we can use the collection' groupBy, map, values, and implode function to get the appropriate data.

$gno = head::select('point', 'DATE')->where('DATE', '>', Carbon::now()->subDays(7))->orderBy('DATE','desc')->get();
$gnoByCount = $gno->groupBy('DATE')->map(function($g) {
    return $g->count();
})->values()->implode(',');

var_dump($gnoByCount);
Sign up to request clarification or add additional context in comments.

11 Comments

i want to return count of record against date in chart
your answer returns sum of count for saven days but i want to print seven days record counts in array For Example ["5","15","20","12","4","2","4"]
No, it will return the array where each element return the number of records for that particular date.
Just remote this ->values()->implode(','); and confirm the records are coming date wise and key will be the date.
It is var_dump not var_dumb
|
2

If you just want an array of the points, then you can use pluck:

$gno = head::select('point')
        ->where('DATE', '>', Carbon::now()->subDays(7))
        ->orderBy('DATE','desc')
        ->get();

$gno->pluck('points')->toArray();

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.