PHP code example of mpociot / cockpit

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

    

mpociot / cockpit example snippets


use Mpociot\Cockpit\Metric;

class Logins extends Metric
{
	// Visible name when selecting the metric in the UI
	protected $name = "My Metric";
	
	// Define all submetrics and which charts are available for each submetric
	protected $allowedSubMetrics = [
        'per_user' => ['LineChart','ColumnChart','PieChart'],
    ];

    // Define all filters that can be applied to this metric
    protected $allowedFilters = [
        'username' => [
            'type' => 'text',
            'name' => 'Username'
        ]
    ];
	
	/**
	 * Implement date filter functionality.
	 * This is needed for all metrics, as the time filter is always available
	 */
    protected function filterFromDate($query, $value)
    {
        return $query->where("created_at" , ">", $value);
    }

    protected function filterUntilDate($query, $value)
    {
        return $query->where("created_at" , "<=", $value);
    }
    
    /**
     * Implement your custom submetrics
     */
     public function calculatePerUser($filters = [])
    {
		// Use this Eloquent model for all queries
        $query = \App\Models\Login::query();
        
		// Apply each filter method on the query object
        $query = $this->applyFilters($query, $filters);

		// Get the results we need to display
        $results = $query
	        ->groupBy("username")
	        ->get([
                DB::raw('username, COUNT(*) AS `count`')
            ]);
		
		// Define the columns our chart will have
        $this->dataTable
        		->addStringColumn("Username")
            	->addNumberColumn("Num Logins");

		// Add the database results to our chart
        foreach ($results as $result) {
            $this->dataTable->addRow([
           	   $result->username,
                $result->count
            ]);
        }

        return $this->dataTable;
    }
     
     /**
     * Implement your custom filters
     */
	protected function filterUsername($query, $value)
    {
        return $query->where("username",$value);
    }
     
}
bash
php artisan vendor:publish --provider="Mpociot\Cockpit\CockpitServiceProvider"
php artisan migrate