1. Go to this page and download the library: Download square1/nova-metrics 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/ */
square1 / nova-metrics example snippets
use Square1\NovaMetrics\CustomTrend;
use Square1\NovaMetrics\CustomValue;
use Square1\NovaMetrics\CustomPartition;
namespace App\Nova\Metrics;
use App\Post;
use Square1\NovaMetrics\CustomTrend;
use Laravel\Nova\Http\Requests\NovaRequest;
class NewTrend extends CustomTrend
{
public function calculate(NovaRequest $request)
{
// $request->filters
}
}
namespace App\Nova\Filters;
use Illuminate\Http\Request;
use Laravel\Nova\Filters\Filter;
class CategoryFilter extends Filter
{
public $component = 'select-filter';
public function apply(Request $request, $query, $value)
{
return $query->where('category', $value);
}
public function options(Request $request)
{
return [
'css' => 'css',
'javascript' => 'javascript',
'laravel' =>'laravel',
'php' => 'php',
];
}
}
class NewTrend extends CustomTrend
{
public function calculate(NovaRequest $request)
{
$model = Post::make();
if (!empty($filters)) {
if ($request->has('filters')) {
// Get the decoded list of filters
$filters = json_decode(base64_decode($request->filters));
foreach ($filters as $filter) {
if (empty($filter->value)) {
continue;
}
// Create a new instance of the filter and apply the query to your model
$model = (new $filter->class)->apply($request, $model, $filter->value);
}
}
}
return $this->averageByDays($request, $model, 'pageviews');
}
}