Laravel visitors counter

phplaravelcountervisitsvisitorvisitors

Last Update : 2023-09-22 UTC 12:04:51 PM

Answers of > Laravel visitors counter

w3coded visits visitor w3coded visits visitor Meta Stack Overflow w3coded visits visitor ,Stack Overflow en español,Stack w3coded visits visitor Overflow em Português,i added the following w3coded visits visitor code to the web.php file above all the routes to w3coded visits visitor run on each request on the site so no matter w3coded visits visitor what page the visitor landed on it will save the w3coded visits visitor ip addess to the database only if its unique so w3coded visits visitor one visitor wont keep addding to the visitor w3coded visits visitor count

This is just for ease: make an Eloquent model for the table so you don't have to use Fluent (query builder) all the time:

class Tracker extends Eloquent {

    public $attributes = [ 'hits' => 0 ];

    protected $fillable = [ 'ip', 'date' ];
    protected $table = 'table_name';

    public static function boot() {
        // Any time the instance is updated (but not created)
        static::saving( function ($tracker) {
            $tracker->visit_time = date('H:i:s');
            $tracker->hits++;
        } );
    }

    public static function hit() {
        static::firstOrCreate([
                  'ip'   => $_SERVER['REMOTE_ADDR'],
                  'date' => date('Y-m-d'),
              ])->save();
    }

}

Now you should be able to do what you want by just calling this:

Tracker::hit();

Looking at your code and reading your description, I’m assuming you want to calculate number of hits from an IP address per day. You could do this using Eloquent’s updateOrNew() method:

$ip = Request::getClientIp();
$visit_date = Carbon::now()->toDateString();

$visitor = Visitor::findOrNew(compact('ip', 'visit_date'));
$visitor->increment('hits');

However, I would add this to a queue so you’re not hitting the database on every request and incrementing your hit count can be done via a background process:

Queue::push('RecordVisit', compact('ip', 'visit_date'));

In terms of where to bootstrap this, the App::before() filter sounds like a good candidate:

App::before(function($request)
{
    $ip = $request->getClientIp();
    $visit_date = Carbon::now()->toDateString();

    Queue::push('RecordVisit', compact('ip', 'visit_date'));
);

The solution:

Tracker::hit();

And a new class:

<?php

class Tracker Extends Eloquent {

    public $attributes = ['hits' => 0];

    protected $fillable = ['ip', 'date'];

    public $timestamps = false;

    protected $table = 'visitor';

    public static function boot() {
        // When a new instance of this model is created...
        static::creating(function ($tracker) {
            $tracker->hits = 0;
        } );

        // Any time the instance is saved (create OR update)
        static::saving(function ($tracker) {
            $tracker->visit_date = date('Y-m-d');
            $tracker->visit_time = date('H:i:s');
            $tracker->hits++;
        } );
    }

    // Fill in the IP and today's date
    public function scopeCurrent($query) {
        return $query->where('ip', $_SERVER['REMOTE_ADDR'])
                     ->where('date', date('Y-m-d'));
    }

    public static function hit() {
        static::firstOrCreate([
                  'ip'   => $_SERVER['REMOTE_ADDR'],
                  'date' => date('Y-m-d'),
              ])->save();
    }
}

public $attributes = ['hits' => 0];

protected $fillable = ['ip', 'date'];

public $timestamps = false;

protected $table = 'trackers';

public static function boot() {
    // When a new instance of this model is created...
    parent::boot();
    static::creating(function ($tracker) {
        $tracker->hits = 0;
    } );

    // Any time the instance is saved (create OR update)
    static::saving(function ($tracker) {
        $tracker->visit_date = date('Y-m-d');
        $tracker->visit_time = date('H:i:s');
        $tracker->hits++;
    } );
}

// Fill in the IP and today's date
public function scopeCurrent($query) {
    return $query->where('ip', $_SERVER['REMOTE_ADDR'])
                 ->where('date', date('Y-m-d'));
}

public static function hit() {
   /* $test= request()->server('REMOTE_ADDR');
    echo $test;
    exit();*/
    static::firstOrCreate([
              'ip'   => $_SERVER['REMOTE_ADDR'],

              'date' => date('Y-m-d'),
             // exit()
          ])->save();

}

i added the following code to the web.php file above all the routes to run on each request on the site so no matter what page the visitor landed on it will save the ip addess to the database only if its unique so one visitor wont keep addding to the visitor count

// Web.php
use App\Models\Visitor

$unique_ip = true;
$visitors = Visitor::all();
foreach($visitors as $visitor){
    if($visitor->ip_address == request()->ip()){
        $unique_ip = false;
    }
}
if($unique_ip == true){
    $visitor = Visitor::create([
        'ip_address' => request()->ip(),
    ]);
}

Routes...

Current topics : Laravel visitors counter

Newly Added Questions

Similar Questions

Questions :

How To Group Array Key Value

Last Update : 2023-09-22 UTC 13:36:11 PM

Questions :

PhpStorm Warning For React Attributes In Jsx File With SCSS File

Last Update : 2023-09-22 UTC 13:35:51 PM

Questions :

Why Is The File Not Showing Up In Request.files And In Request.forms Instead?

Last Update : 2023-09-22 UTC 13:35:43 PM

Questions :

Proxying Assets From React App Directory In Slim Framework?

Last Update : 2023-09-22 UTC 13:35:32 PM

Questions :

Laravel 5.4 Can't Run “php Artisan Preset React” Comand

Last Update : 2023-09-22 UTC 13:35:25 PM

Questions :

How To Update Session Values Without Signing Out?

Last Update : 2023-09-22 UTC 13:35:05 PM

Questions :

Array Is Not Visible

Last Update : 2023-09-22 UTC 13:34:55 PM

Questions :

React Routing For Login Using Symfony

Last Update : 2023-09-22 UTC 13:34:44 PM

Questions :

Sanctum With React SPA Returning 419 Page Expired

Last Update : 2023-09-22 UTC 13:34:30 PM

Questions :

How Do I Import An Input String Into Another Page

Last Update : 2023-09-22 UTC 13:34:23 PM

Top
© 2023 W3CODED - All Rights Reserved.