PHP code example of hms / statistics

1. Go to this page and download the library: Download hms/statistics library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

hms / statistics example snippets


use Hms\Statistics\Eloquents\Statistics;
use Jenssegers\Mongodb\Eloquent\Model;

class Example extends Model
{
    use Statistics;

    public function __construct() 
    {
        parent::__construct();
        
        
        $this->statisticsConditionFields = [
            'day'
        ];
        
        $this->incrementFields = [
            'number'
        ];
    }
}

use App\Example;

$example = new Example();
$example->statistics([
    'day'       => Carbon::now()->toDateString(),
    'number'    => 1
]);

$example->cache([
    'day'       => Carbon::now()->toDateString(),
    'number'    => 3
]);

'namespaces' => [
    App\Example::class
]

protected function schedule(Schedule $schedule)
{
    $schedule->command('statistics:sync')->daily();
}

$example->setDistinct([
    'client' => 'ios',
    'device_id' => 'balabala...' 
]);

use Hms\Statistics\Models\StatisticsLog;

// 设置去重条件
$example->setDistinct(function () {
    return Example::query()->where('client', 'ios')->whereIn('user_id', 1234)->first();
});

// 此时应该这样调用cache
$example->cache([
    'day'       => Carbon::now()->toDateString(),
    'number'    => 3
], function () {
    StatisticsLog::create([
        'client'    => 'ios',
        'user_id'   => 1234
    ]);
});

php artisan vendor:publish --tag=statistics-config