Ready to use Laravel statistic package...
Get via composer
composer require laravel-ready/statistics
php artisan vendor:publish --tag=statistics-config
# publish migrations
php artisan vendor:publish --tag=statistics-migrations
# apply migrations
php artisan migrate --path=/database/migrations/laravel-ready/statistics
Add Statisticable
trait to your model
use LaravelReady\Statistics\Traits\Statisticable;
class Post extends Model
{
use Statisticable;
}
Then call touch
or hit
method to run statistic mechanism
use LaravelReady\Statistics\Supports\Statistic;
Statistic::touch($model); // process data then save it
Statistic::hit($model); // just saves raw data without processing (then you can process it later with jobs)
Access statistic data
$stats = $model->load('statistics');
Process statistic data with scheduled command (or you can use LaravelReady\Statistics\Jobs\ProcessStatistic
job)
<?php
namespace App\Console;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('statistics:process')->everyMinute();
}
...
İf you want to use ip2location database, you can use docker image.
Define your environment variables in .env
file (your laravel project)
# ip2location
IP2LOCATION_TOKEN={YOUR_TOKEN}
IP2LOCATION_CODE=DB11-LITE
IP2LOCATION_IP_TYPE=BOTH
IP2LOCATION_MYSQL_PORT=1010
IP2LOCATION_MYSQL_HOST=127.0.0.1
IP2LOCATION_MYSQL_DBNAME=ip2location_database
IP2LOCATION_MYSQL_USERNAME=admin
IP2LOCATION_MYSQL_PASSWORD=secret
Create docker-compose.yml
file in your laravel project
version: "3.8"
services:
# ip2location server
ip2location_mysql:
image: ip2location/mysql:latest
container_name: ip2location_mysql
restart: unless-stopped
ports:
- ${IP2LOCATION_MYSQL_PORT}:3306
environment:
TOKEN: ${IP2LOCATION_TOKEN}
CODE: ${IP2LOCATION_CODE}
IP_TYPE: ${IP2LOCATION_IP_TYPE}
MYSQL_PASSWORD: ${IP2LOCATION_MYSQL_PASSWORD}
networks:
- mysql_net
networks:
mysql_net:
driver: bridge
Run docker containers
docker-compose up -d
Then wait for ip2location database to be downloaded (this may some time).
- This project was generated by the packager.