Giter Club home page Giter Club logo

statistics's Introduction

Statistics

Allows you to extract a Illuminate\Support\Collection of statistics about a set of dated Eloquent rows.

Features

  • Based on Carbon : for easy date management and for an easy integration with Eloquent
  • A simple Cache : for avoiding useless dupliated database queries
  • Custom interval and steps : allows you to change dates (start, end) and the steps (yearly, monthly, daily, hourly)
  • Return a Collection : a Illuminate\Support\Collection is returned for a simpler statistics handling
  • Indicator based : for a simple and comprehensive way of querying statistics datas

Installation

Install it with composer :

composer require ifnot/statistics

Usage

1 : Instanciate a Statistics object from a eloquent query :

use Ifnot\Statistics\Statistics;

// Get all validated sales (you can also take all with Sale::query())
$validSales = Sale::whereNotNull('validated_at');

// Put our sales into a Statistics object
$statistics = Statistics::of($validSales);

2 : Specify the date column and the interval for filtering datas :

use Ifnot\Statistics\Interval;

// If we want to build statistics about validation date of sales (it can be also of the creation date, 
// in this case we will use the eloquent created_at ...) 
$statistics->date('validated_at');

// Set the interval, the params :
// 1 : the interval, check the constants Interval::$DAILY, Interval::$MONTHLY etc ... or use the string "daily", "monthly" instead
// 2 : the start date with carbon
// 3 : the end date with carbon
$statistics->interval(Interval::$DAILY, Carbon::createFromFormat('Y-m-d', '2016-01-01'), Carbon::now())

3 : Add indicators :

// We want to count the sales with shipping and without shipping
$statistics->indicator('with_shipping', function($row) {
 return $row->shipping ? 1 : 0;
});
$statistics->indicator('without_shipping', function($row) {
 return $row->shipping ? 0 : 1;
});

// And count the sales with more than 500.00 € amount
$statistics->indicator('expensive_bough', function($row) {
 if($row->amount > 500.00) {
  return 1;
 } else {
  return 0;
 }
});

4 : Handle datas :

$collection = $statistics->make();

// Use a foreach if you want to loop on each dates
foreach($collection as $date => $values) {
 echo $date ' : ' . $values->expensive_bough;
}

// Use Collection methods for statistics
$collection->sum('with_shipping'); // Count the shipping
$collection->avg('with_shipping'); // Average shpping sales on each days on the interval (if you selected daily)
$collection->min('with_shipping'); // Minimum daily shipping on the interval
$collection->max('with_shipping');

// etc ...

Full Documentation

... Work here

statistics's People

Contributors

ifnotfr avatar teeqz avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.