Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Generowanie statystyk w Laravel
trifek
post 15.10.2019, 12:10:15
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


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
Go to the top of the page
+Quote Post
markonix
post 19.10.2019, 00:57:05
Post #2





Grupa: Zarejestrowani
Postów: 2 707
Pomógł: 290
Dołączył: 16.12.2008
Skąd: Śląsk

Ostrzeżenie: (0%)
-----


Ś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.


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 19.04.2024 - 21:24