我想以干净的方式显示查询中的所有数据。所以我想要这样的输出:
[
{"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}
]
变成这样:
["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]
以便所有值都适用于我的 javascript 代码。
我的查询是这样的:
$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();
我在 Laravel View 中获得的数据只是查询的输出,所以
{{ $stats }}
是我的看法。问题是,如何按照我想要的方式输出值? (见上文)
谢谢!
最佳答案
最快、最简单的方法就是按照您的需要在 View 中打印数组:
@foreach ($stats as $stat)
["{{ date('d/m', strtotime($stat['date'])) }}", {{ $stat['count(ip)'] }}],
@endforeach
关于javascript - Laravel 将所有数据显示在一个干净的数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30129111/