Robin
Robin

Reputation: 1587

Laravel display all data in a clean array

I want to display all data from a query in a clean manner. So I want to have this output:

[
{"date":"2015-05-01","count(ip)":1},
{"date":"2015-05-02","count(ip)":1},
{"date":"2015-05-03","count(ip)":3},
{"date":"2015-05-04","count(ip)":1},
{"date":"2015-05-06","count(ip)":2},
{"date":"2015-05-07","count(ip)":1},
{"date":"2015-05-08","count(ip)":1}
]

To become this:

["2015-05-01", 1],
["2015-05-02", 1],
["2015-05-03", 3],
["2015-05-04", 1],
["2015-05-06", 2],
["2015-05-07", 1],
["2015-05-08", 1]

So that all the values work on my javascript code.

My query is this:

$begin = date('Y-m-01');
$end = date('Y-m-t');

$visits = Tracker::selectRaw('date, count(ip)')->groupBy('date')->whereRaw("date between '$begin' and '$end'")->get();

And the data I've got in my Laravel view is just the output of my query so {{ $stats }} Is what I have in my view. The question is, how can I output the values the way I want? (see above)

Thanks!

Upvotes: 0

Views: 170

Answers (1)

Laurence
Laurence

Reputation: 60048

The quickest and easiest way is just print the array inside your view exactly as you need it:

 @foreach ($stats as $stat)
      ["{{ date('d/m', strtotime($stat['date'])) }}", {{ $stat['count(ip)'] }}],
 @endforeach

Upvotes: 1

Related Questions