PHP code example of asivas / analytics-dashboard

1. Go to this page and download the library: Download asivas/analytics-dashboard 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/ */

    

asivas / analytics-dashboard example snippets


   
   namespace App\Facades;
   use App\Analytics\SomeMetricsIndicator;
   
   class Analytics
   {            
       /** 
        ** IMPORTANT ** This method must be present, otherwise getWidgetData won't be able to build the proper response
        */
       public function getWidget($analyticsName):Widget {
            $dwc = new DashboardWidgetController();
            return $dwc->getWidget($analyticsName);
        }
       //** */

         public function someMetricsIndicator($from,$to,$params = null)
         {
           return SomeMetricsIndicator::getData($from,$to,$params);
         }
        // TODO: create the methods similiar to someMetricsIndicator with your criteria
   }
   

   namespace App\Http\Controllers\Dashboard;
   
   
   use Asivas\Analytics\Http\Controllers\Dashboard\DashboardWidgetController as AsivasDashboardWidgetController;
   
   class DashboardWidgetController extends AsivasDashboardWidgetController
   {
   ....
   }

   use App\Facades\Analytics;
   use App\Http\Controllers\Dashboard\DashboardWidgetController;
   
   class AppServiceProvider extends ServiceProvider
    {
    /**
      * Register any application services.
      * 
      * @return void
      */
     public function register()
     {
        //
        $this->app->bind('Analytics', function($app) {
           return new Analytics();
        });
   
         $this->app->bind('WidgetController', function($app) {
            return new DashboardWidgetController();
        });
     }
   ...
   

   'aliases' => [
   ...
     'Analytics' => \Asivas\Analytics\AnalyticsFacade::class,
     'WidgetController' => \Asivas\Analytics\WiggetControllerFacade::class
   ...
    

   
    namespace App\Analytics;
    
    use App\Models\Report;
    use Asivas\Analytics\Analytics;
    use Illuminate\Database\Eloquent\Builder;
    
    class SomeMetricsIndicator extends Analytics
    {
        protected $mainModel = SomeModel::class;
    
        protected function addJoins(Builder $q)
        {
            return $q->selectRaw('COUNT(some_related_model.created_at) as somemodels, \'assigned\' as status')
                ->join('some_related_models','somemodels.id', '=','some_related_models.somemodel_id')
                ->join('other_related_models','somemodels.id', '=','other_related_models.somemodel_id')
                ->join('related_to_others','other_related_models.related_to_other_id', '=','related_to_others.id')
                ->join('users','related_to_others.user_id', '=','users.id');
        }
    
        protected function addGroupBys(Builder $q)
        {
            return $q->groupBy('status');
        }
    
        protected function addWheres(Builder $q, $from, $to)
        {
            return $q->whereHas('assignations',function ($q)
                        use($from,$to){
                            $q->whereNull('rejected_date')
                                ->where('users.id',request('user'));
                        });
        }
    }