A Laravel Statistics SDK

Overview

laravel-statistics

shanjing laravel-statistics是一个基于 laravel 开发而成的统计工具,只需很少的代码即可快速构建出一个功能完善的统计模块。开箱即用,对后端开发者非常友好。

功能特性

  • 简洁优雅 API
  • 当缺失对应日期的数据时,自动补充 0 作为默认数据
  • json 格式存储数据,易于扩展字段, 存储更优雅

环境

  • PHP > 7.4
  • Laravel 8.*
  • MySQL 8.*

安装

如果安装过程中出现composer下载过慢或安装失败的情况,请运行命令composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/composer镜像更换为阿里云镜像。

首先需要安装laravel框架,如已安装可以跳过此步骤。如果您是第一次使用laravel,请务必先阅读文档 安装 《Laravel中文文档》

composer create-project --prefer-dist laravel/laravel 项目名称 7.*
#
composer create-project --prefer-dist laravel/laravel 项目名称

安装完laravel之后需要修改.env文件,设置数据库连接设置正确

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database
DB_USERNAME=root
DB_PASSWORD=

安装laravel-statistics

cd {项目名称}
$ composer require shanjing/laravel-statistics -vvv

然后运行下面的命令来发布资源:

php artisan laravel-statistics:publish

在该命令会生成配置文件config/statistics.php,可以在里面修改数据库连接、以及表名,建议都是用默认配置不修改。


// 指定数据库链接.
'connection' => '',

// 指定表名,如果后期修改数据库表名,这里也需要修改成对应的表名.
'statistics_table' => 'laravel_statistics',

然后运行下面的命令完成安装:

  • 注意: 由于运行安装是针对具体的迁移文件运行的, 迁移文件名不可以修改。
    • 文件:"2021_09_22_083561_create_statistics_table.php",
    • 目录:项目目录/database/migrations/
php artisan laravel-statistics:install

使用

上述步骤操作完成之后就可以使用统计功能了

  • 使用统计表统计数据和读取数据

读取数据

// 今天淘宝销量、销售额
// period   year | month | week | day
// orderBy  desc | asc
app('statistics')
->get("taobao", ['gmv', 'order_num'])
->period('day')
->orderBy("desc")
->exec();

// 20210901~20210921 淘宝销量、销售额
app('statistics')
->get("taobao", ['gmv', 'order_num'])
->occurredBetween([20210901, 20210921])
->period('day')
->orderBy("desc") 
->exec();

存储数据

// 更新数据
// $arr: 会已 json 的格式存储在 data 列
// taobao: 存储在 key 列
// occurredAt: 存储在 occurred_at 列
$arr = ['gmv'=>'value', 'order_num'=>'value'];
app('statistics')
->save("taobao", $arr)
->occurredAt(20210921)
->exec();
  • 使用已有的 model 展示实时数据

1)向需要统计功能的 model 引入 statistics 模块

<?php
  
namespace App\Models\Order;
  
use Illuminate\Database\Eloquent\Model;
use Shanjing\LaravelStatistics\Traits\Statistics;
  
class Order extends Model
{ 
    use Statistics;
  
    protected $table = '';
  
    protected $connection = '';
}

2)在调用的 controller 里,调用

// period   year | month | week | day
// occurredBetween 时间范围
// orderBy  desc | asc
Order::period("day")
   ->occurredBetween(["20210718", "20210921"])
   ->selectRaw('count(id) as total') // 支持 select 语句
   ->selectRaw('SUM(`price`) as gmv') // 支持 select 语句
   ->orderBy('desc')
   ->summary();

Contributors

This project exists thanks to all the people who contribute. [Contribute].

You can contribute in one of three ways:

  1. File bug reports using the issue tracker.
  2. Answer questions or fix bugs on the issue tracker.
  3. Contribute new features or update the wiki.

The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable.

License

MIT

You might also like...
Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application.

Laravel Segment Laravel Segment is an opinionated, approach to integrating Segment into your Laravel application. Installation You can install the pac

Laravel Sanctum support for Laravel Lighthouse
Laravel Sanctum support for Laravel Lighthouse

Lighthouse Sanctum Add Laravel Sanctum support to Lighthouse Requirements Installation Usage Login Logout Register Email Verification Forgot Password

Bring Laravel 8's cursor pagination to Laravel 6, 7

Laravel Cursor Paginate for laravel 6,7 Installation You can install the package via composer: composer require vanthao03596/laravel-cursor-paginate U

A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.
A package that uses blade templates to control how markdown is converted to HTML inside Laravel, as well as providing support for markdown files to Laravel views.

Install Install via composer. $ composer require olliecodes/laravel-etched-blade Once installed you'll want to publish the config. $ php artisan vendo

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic

A light weight laravel package that facilitates dealing with arabic concepts using a set of classes and methods to make laravel speaks arabic! concepts like , Hijri Dates & Arabic strings and so on ..

Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

Laravel Jetstream is a beautifully designed application scaffolding for Laravel.

Laravel Jetstream is a beautifully designed application scaffolding for Laravel. Jetstream provides the perfect starting point for your next Laravel application and includes login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management.

Laravel Larex lets you translate your whole Laravel application from a single CSV file.
Laravel Larex lets you translate your whole Laravel application from a single CSV file.

Laravel Larex Translate Laravel Apps from a CSV File Laravel Larex lets you translate your whole Laravel application from a single CSV file. You can i

A Laravel package that adds a simple image functionality to any Laravel model
A Laravel package that adds a simple image functionality to any Laravel model

Laraimage A Laravel package that adds a simple image functionality to any Laravel model Introduction Laraimage served four use cases when using images

Owner
青岛山景信息技术有限公司
青岛山景信息技术有限公司
Log requests and group together for aggregated statistics of route usage

Log Laravel route usage statistics Log Laravel requests and responses for statistical purposes and optionally aggregate by hours/days/months for minim

Bilfeldt 108 Dec 11, 2022
Laravel mercado pago es un paquete que te ayuda a implementar el sdk de mercado pago para php en laravel

Introducción Laravel mercado pago es un paquete que te ayuda a implementar el sdk de mercado pago para php en laravel. ?? Instalación Para instalar ut

null 7 Sep 23, 2022
Video Chat application built using Metered Video SDK, with PHP Laravel Backend and JavaScript Front-End

Group Video Chat App with PHP Laravel and JavaScript Powered by Metered Video SDK Overview This application is a highly scalable group video calling a

null 2 Aug 18, 2022
Easy-to-use SDK for implementing Neshan APIs in your Laravel projects.

Neshan Laravel SDK Easy-to-use SDK for implementing Neshan APIs in your Laravel projects. Install The easiest way to install is by using Composer: com

null 1 Oct 22, 2022
A Laravel package for the Adapty SDK.

Laravel Adapty A Laravel package for the Adapty SDK. Please feel free to contribute... Installation You can install the package via composer: composer

Melih Berat ŞANLI 2 Sep 20, 2022
可能是我用过的最优雅的 Alipay 和 WeChat 的支付 SDK 扩展包了

前言 v3 版与 v2 版在底层有很大的不同,基础架构做了重新的设计,更易扩展,使用起来更方便。 开发了多次支付宝与微信支付后,很自然产生一种反感,惰性又来了,想在网上找相关的轮子,可是一直没有找到一款自己觉得逞心如意的,要么使用起来太难理解,要么文件结构太杂乱,只有自己撸起袖子干了。 欢迎 Sta

yansongda 4.5k Jan 6, 2023
List of 77 languages for Laravel Framework 4, 5, 6, 7 and 8, Laravel Jetstream , Laravel Fortify, Laravel Breeze, Laravel Cashier, Laravel Nova and Laravel Spark.

Laravel Lang In this repository, you can find the lang files for the Laravel Framework 4/5/6/7/8, Laravel Jetstream , Laravel Fortify, Laravel Cashier

Laravel Lang 6.9k Jan 2, 2023
⚡ Laravel Charts — Build charts using laravel. The laravel adapter for Chartisan.

What is laravel charts? Charts is a Laravel library used to create Charts using Chartisan. Chartisan does already have a PHP adapter. However, this li

Erik C. Forés 31 Dec 18, 2022
Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster.

Laravel Kickstart What is Laravel Kickstart? Laravel Kickstart is a Laravel starter configuration that helps you build Laravel websites faster. It com

Sam Rapaport 46 Oct 1, 2022
Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

Laravel User Activity Log - a package for Laravel 8.x that provides easy to use features to log the activities of the users of your Laravel app

null 9 Dec 14, 2022