简体   繁体   English

Laravel从数据库中获取所有数据并将其放入图表中

[英]Laravel get all data from database and put it into a chart

I'm working with Laravel 4 to build an applicaton. 我正在与Laravel 4合作构建一个应用程序。

Now, I do count all my visitors in a 'unique' way. 现在,我确实以一种“独特”的方式统计了所有访客。 So the system does chack if there was already a visit this day and if so, it just counts a click in the database. 因此,如果这一天已经来过一次访问,那么系统确实会窃听,如果是,那么它只计算数据库中的一次点击。

What my database structure look like: 我的数据库结构如下所示:

结构体

I want the system to count the visitors (so the IP adressess) of day X in month X. Like the visitors of 30/04/2015 wich is in my table 2015-04-30 . 我希望系统计算X月第X天的访问者(因此IP地址)。就像30/04/2015的访问者在我的表2015-04-30 I want to count all the IP adressess of that day and the other days of a month. 我想计算当天和每个月其他几天的所有IP地址。 But when the month is over, it needs to get the values of the next month, so I want the values of the current month. 但是当月份结束时,它需要获取下个月的值,因此我需要当前月份的值。

The hits are just to count the 'clicks' on my site, so don't need to mention that. 这些hits只是为了统计我网站上的“点击”,因此无需提及。

The date and visit_date give me the current date of visiting and visit_time gives me the time of the visit. date和visit_date给我当前的访问日期,而visit_time给我访问的时间。

My current code looks like this to display the values: 我当前的代码如下所示以显示值:

public function index()
{

    $select_stats = DB::table('visitor')->whereBetween('visit_date', array(date('Y-m-01'), date('Y-m-t')))->get();

    $month = date('m');

    $get_count = DB::select(DB::raw("SELECT count(*) FROM visitor WHERE MONTH(date) = $month GROUP BY DAY(date)"));


    foreach($select_stats as $statics) {

   //change our object to an array
   $statics = array();
    }


return View::make('admin.home.index')->with('stats', $select_stats)/*->with('count', $get_count)*/;

}

But When I try to display this code, with the counted numbers, it doesn't work anymore. 但是,当我尝试显示带有已计数数字的代码时,它将不再起作用。 My view looks like this: 我的看法如下:

<script type="text/javascript">
    var visitors = [
@foreach($stats as $stat)
                ['{{ date("d/m/Y", strtotime($stat->date)) }}', 55],
@endforeach
            ];
</script>

So the 55 inside of my view are the counted visitors as output. 因此,我视图中的55是被计为输出的访问者。

Could someone help me as soon as possible please? 有人可以尽快帮助我吗?

Kindest regards, 最亲切的问候,

Robin 罗宾

I'm not really understanding all that well, but a few things that could help... 1. I'd really suggest looking into using model classes in stead of using DB raw queries. 我并不是很了解所有这些,但是有些事情可能会有所帮助... 1.我真的建议您考虑使用模型类,而不是使用数据库原始查询。 Thats where Laravel is really great. 那就是Laravel真正很棒的地方。 For example it would be: 例如,它将是:

$select_stats= Visitor::where('visit_date', '>=', Carbon::now()->startOfMonth())->get();
$get_count = $select_stats->count();
  1. In stead of doing a foreach in the script, I'd return a json array. 代替在脚本中执行foreach,我将返回一个json数组。 So you could send the 'stats' variable to your view instead as: 因此,您可以将“ stats”变量发送给视图,而不是:

    with('stats', $select_stats->lists('visit_date')) with('stats',$ select_stats-> lists('visit_date'))

and then in the script you'd just have to use: 然后只需在脚本中使用:

{{ json_encode($stats) }}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM