Php 在Laravel中生成统计信息
我是拉拉维尔的初学者。我在我的项目中使用了Laravel 5.8 我有一个模式:Php 在Laravel中生成统计信息,php,laravel,laravel-5,Php,Laravel,Laravel 5,我是拉拉维尔的初学者。我在我的项目中使用了Laravel 5.8 我有一个模式: Schema::create('statistics', function (Blueprint $table) { $table->bigIncrements('id'); $table->bigInteger('company_id')->unsigned(); $table->foreign('company_id')->references('id')-
Schema::create('statistics', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('company_id')->unsigned();
$table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
$table->text('agent')->nullable();
$table->date('date')->nullable();
$table->ipAddress('ip');
$table->bigInteger('user_id')->default(0);
$table->bigInteger('quest_id')->default(0);
$table->string('browser', 70)->nullable();
$table->string('platform', 70)->nullable();
$table->string('language', 12)->nullable();
$table->engine = "InnoDB";
$table->charset = 'utf8mb4';
$table->collation = 'utf8mb4_unicode_ci';
});
我从该函数获取统计信息:
public function generateStatistics(string $dateFrom, string $dateTo, int $id)
{
return Statistics::whereBetween('date', [$dateFrom, $dateTo])->where('user_id', $id)->get();
}
$statisticsTotal = $this->frontendRepository->generateStatistics($dateFrom, $dateTo, $request->user()->id);
$statisticsResultsTotal = [];
$period = CarbonPeriod::create($dateFromInput, '1 day', $dateToInput);
foreach ($period as $dt) {
$date = $dt->format("Y-m-d");
$count = 0;
$count2 = 0;
foreach ($statisticsTotal as $stat){
if ($stat->date == $date){
$count = $count + 1;
}
}
array_push($statisticsResultsTotal, "$date|$count");
};
此代码生成统计信息。很好。
现在我需要生成唯一的访问统计信息
唯一=唯一IP
我该怎么做呢?如果你想继续使用你的逻辑,一个简单的groupBy就足够了 您可以为其生成另一个函数,如下所示:
public function generateUniqueStatistics(string $dateFrom, string $dateTo, int $id)
{
return Statistics::whereBetween('date', [$dateFrom, $dateTo])->where('user_id', $id)->groupBy('ip')->get();
}
return Statistics::whereBetween('date', [$dateFrom, $dateTo])->where('user_id', $id)->select('company_id','agent','ip')->groupBy('ip')->get();
这样,您只需将呼叫更改为:
$statisticsTotal = $this->frontendRepository->generateUniqueStatistics($dateFrom, $dateTo, $request->user()->id);
rest代码将以相同的格式处理它
更新
我认为这是显而易见的,但为了让groupBy运行而不包含select列,您需要在非严格模式下运行mySql
在config/database.php
文件中,您会发现一个数组connections
,其中有一个键mysql
,将其属性strict
设置为false
如果您不想这样做并继续使用严格的mysql,只需提供您感兴趣的列名称来选择,并且不包括id,因为在选择按ip分组的统计信息时,id在这里不相关
例如,假设您需要company\u id
和agent
,您可以这样做:
public function generateUniqueStatistics(string $dateFrom, string $dateTo, int $id)
{
return Statistics::whereBetween('date', [$dateFrom, $dateTo])->where('user_id', $id)->groupBy('ip')->get();
}
return Statistics::whereBetween('date', [$dateFrom, $dateTo])->where('user_id', $id)->select('company_id','agent','ip')->groupBy('ip')->get();
我希望这对你的回答有帮助。此函数返回给我错误:SQLSTATE[42000]:语法错误或访问冲突:1055'db.statistics.id'不在分组依据中(SQL:select*from
statistics
wheredate
2019-10-15 00:00:00到2019-10-22 00:00:00:00和user_id
=2 groupbyip
)谢谢。当我启用严格模式并运行您的代码时-我有错误:SQLSTATE[42000]:语法错误或访问冲突:1055“db.statistics.company_id”不在GROUP BY中(SQL:2019-10-15 00:00:00到2019-10-22 00:00:00之间的公司id
,代理
,ip
,其中日期
,以及用户id
=2分组依据ip
)。我更愿意启用此选项。当我将false设置为Stricklands模式时,我得到的结果0不确定这是怎么可能的:)你能用数据库中的一些条目更新你的问题吗?像几行一样,可以随意更改值,我只想看看它