0

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!

1

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the amazing quick response! This works for me, exept I want the date (yyyy-mm-dd) to be changed to dd/mm Can I do this via Javascript?
If my answer has helped you - please dont forget to upvote and accept it. Thanks :)

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.