Php Laravel从数据库中获取最多信息
我在Laravel 4中有一个跟踪器,我想知道如何从我的表格中获得“最多” 所以我想要的是有一天有最多的游客 我的表结构如下所示:Php Laravel从数据库中获取最多信息,php,laravel-4,tracker,Php,Laravel 4,Tracker,我在Laravel 4中有一个跟踪器,我想知道如何从我的表格中获得“最多” 所以我想要的是有一天有最多的游客 我的表结构如下所示: public function index() { $begin = date('Y-m-01'); $end = date('Y-m-t'); $visits = Tracker::selectRaw('date, count(ip)')->groupBy('date')->whereR
public function index()
{
$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();
//get total visits per month
$get_visits = Visitor::whereRaw("date between '$begin' and '$end'")->count();
// get average visits
// transform dates to DateTime objects (we need the number of days between $begin and $end)
$begin = new \DateTime($begin);
$end = new \DateTime('now');
$diff = $end->diff($begin); // creates a DateInterval object
$days = (int)$diff->format('%a'); // %a --> days
$average_visits = $get_visits / $days;
return View::make('admin.home.index')->with('stats', $visits)
->with('get_visits', $get_visits)
->with('average_visits', $average_visits);
}
因此,我的控制器如下所示:
public function index()
{
$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();
//get total visits per month
$get_visits = Visitor::whereRaw("date between '$begin' and '$end'")->count();
// get average visits
// transform dates to DateTime objects (we need the number of days between $begin and $end)
$begin = new \DateTime($begin);
$end = new \DateTime('now');
$diff = $end->diff($begin); // creates a DateInterval object
$days = (int)$diff->format('%a'); // %a --> days
$average_visits = $get_visits / $days;
return View::make('admin.home.index')->with('stats', $visits)
->with('get_visits', $get_visits)
->with('average_visits', $average_visits);
}
我想要的输出:
2015年6月18日,我们的访客最多(542名)
比如说
谢谢 您可以选择日期按顺序访客数量下降和限制1。这是基本的mysql。如何在laravel中使用此功能,请参见 按描述订购:
->orderBy('visits', 'desc')
限制:
->take(1)
我的示例查询基于一个系统,该系统有一个计算访问量的列(您的结构没有)。所以,让我来帮助你了解你的结构
您希望按日期进行分组,因此基本上可以按日期进行分组:
->groupBy('date')
在此查询中,您希望获得属于该日期的行数。因此,您可以使用计数:
->count()
这都是基于你对查询的了解。这都是根据查询生成器进行的。您可以选择日期按访客数量递减和限制按1排序。这是基本的mysql。如何在laravel中使用此功能,请参见 按描述订购:
->orderBy('visits', 'desc')
限制:
->take(1)
我的示例查询基于一个系统,该系统有一个计算访问量的列(您的结构没有)。所以,让我来帮助你了解你的结构
您希望按日期进行分组,因此基本上可以按日期进行分组:
->groupBy('date')
在此查询中,您希望获得属于该日期的行数。因此,您可以使用计数:
->count()
这都是基于你对查询的了解。这些都是根据查询生成器计算的。是的,我每月都会计算所有访客的数量。。。但我想要的是最大值,明白吗?@RobinR你想要的是访客最多的日期,对吗?@RobinR是的。我的回答给了你所需要的一切。基本上:选择日期和按日期分组的所有记录的计数,按日期描述限制1分组的所有记录的计数排序。你能给我举个例子吗?(在RMO Laravel查询中?@RobinR我会的,当我回家的时候。是的,我每个月都会计算我所有的访客。。。但我想要的是最大值,明白吗?@RobinR你想要的是访客最多的日期,对吗?@RobinR是的。我的回答给了你所需要的一切。基本上:选择日期和按日期分组的所有记录的计数,按日期描述限制1分组的所有记录的计数排序。你能给我举个例子吗?(在RMO Laravel查询中?@RobinR我会的,当我回家的时候。是的,我每个月都会计算我所有的访客。。。但我想要的是最大值,明白吗?@RobinR你想要的是访客最多的日期,对吗?@RobinR是的。我的回答给了你所需要的一切。基本上:选择日期和按日期分组的所有记录的计数,按日期描述限制1分组的所有记录的计数排序。你能给我举个例子吗?(在RMO Laravel查询中?@RobinR我会的,当我回家的时候。