Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Generowanie statystyk w Laravel
Forum PHP.pl > Forum > Przedszkole
trifek
Witajcie.

Mam taki kod:
  1.  
  2. Schema::create('statistics', function (Blueprint $table) {
  3. $table->bigIncrements('id');
  4. $table->bigInteger('company_id')->unsigned();
  5. $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
  6. $table->text('agent')->nullable();
  7. $table->date('date')->nullable();
  8. $table->ipAddress('ip');
  9. $table->bigInteger('user_id')->default(0);
  10. $table->bigInteger('quest_id')->default(0);
  11. $table->string('browser', 70)->nullable();
  12. $table->string('platform', 70)->nullable();
  13. $table->string('language', 12)->nullable();
  14. $table->engine = "InnoDB";
  15. $table->charset = 'utf8mb4';
  16. $table->collation = 'utf8mb4_unicode_ci';
  17. });



Generuje statystyki za pomocą poniższego kodu:
  1.  
  2. public function generateStatistics(string $dateFrom, string $dateTo, int $id)
  3. {
  4. return Statistics::whereBetween('date', [$dateFrom, $dateTo])->where('user_id', $id)->get();
  5. }
  6.  
  7. $statisticsTotal = $this->frontendRepository->generateStatistics($dateFrom, $dateTo, $request->user()->id);
  8. $statisticsResultsTotal = [];
  9.  
  10. $period = CarbonPeriod::create($dateFromInput, '1 day', $dateToInput);
  11. foreach ($period as $dt) {
  12. $date = $dt->format("Y-m-d");
  13. $count = 0;
  14. $count2 = 0;
  15. foreach ($statisticsTotal as $stat){
  16. if ($stat->date == $date){
  17. $count = $count + 1;
  18. }
  19. }
  20. array_push($statisticsResultsTotal, "$date|$count");
  21. };



Powyższy kod generuje mi statystyki poprawnie.
Chciałbym je przerobić na statystyki unikalnych wejść.

Unikalne wejście = unikalne IP.

Wie ktoś może jak to przerobić? smile.gif
markonix
Średni ten skrypt, zmieniłbym bym to na GROUP BY `date` i COUNT i masz gotową strukturę jednym zapytaniem.
Żeby liczyć unikalne można dodać potem DISTINCT.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.