在Laravel中生成统计信息

时间:2019-10-15 11:04:07

标签: php laravel laravel-5

我是Laravel的初学者。我在我的项目Laravel 5.8中使用。

我有模式:

list of lists

我从此函数获得统计信息:

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';
});

此代码生成统计信息。很好 现在,我需要生成唯一的访问统计信息 唯一=唯一IP。

我该怎么做?

1 个答案:

答案 0 :(得分:2)

如果要继续使用相同的逻辑,只需一个简单的 groupBy 就可以了。

您可以为它生成另一个函数,如下所示:

void foobar() {
   someObject.doWork(new UpLoadDataService.UploadCompleteListener() {
   @Override
   public void uploadComplete() {
       //write your logic here
       return Result.success(); 
   }

   @Override
   public void uploadFailed(String reason) {
       //write your logic here
       return Result.failure(); 
   }
   });
}

这样,您可以将呼叫更改为:

public function generateUniqueStatistics(string $dateFrom, string $dateTo, int $id)
{
    return Statistics::whereBetween('date', [$dateFrom, $dateTo])->where('user_id', $id)->groupBy('ip')->get();
}

其余代码将以相同格式使用。

更新

我认为这很明显,但是,为了清楚起见,为了运行 groupBy 而没有其中的 select 列,您将需要运行非严格模式下的mySql。

$statisticsTotal = $this->frontendRepository->generateUniqueStatistics($dateFrom, $dateTo, $request->user()->id); 文件中,您会发现一个数组config/database.php,在其下有一个键connections,将其属性mysql设置为strict

如果您不想这样做并继续使用严格的mysql,请仅提供您想要选择的列名称,并且不包括id,因为在通过ip选择统计分组时,id与此处无关。

例如,假设您需要falsecompany_id,您可以这样做:

agent

我希望对您有帮助