PHP code example of mlsolutions / nova-dashboard

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

    

mlsolutions / nova-dashboard example snippets


use App\Models\User;
use MlSolutions\NovaDashboard\Card\NovaDashboard;
use MlSolutions\NovaDashboard\Card\View;
use MlSolutions\NovaDashboard\Filters;
use Laravel\Nova\Dashboards\Main as Dashboard;

class Main extends Dashboard
{
    public function cards(): array
    {
        return [
            NovaDashboard::make()
                ->addView('Website Performance', function (View $view) {
                    return $view
                        ->icon('window')
                        ->addWidgets([
                            BounceRate::make(),
                            ConversionRate::make(),
                            WebsiteTraffic::make(),
                            SessionDuration::make(),
                        ])
                        ->addFilters([
                            LocationFilter::make(),
                            UserTypeFilter::make(),
                            DateRangeFilter::make(),
                        ])
                        ->download(
                            resolver: function ($request, View $view, string $format, Filters $filters) {
                                return $filters
                                    ->applyToQueryBuilder(User::query())
                                    ->select('name', 'email', 'created_at');
                            },
                            label: 'Download Report',
                            filename: 'website-performance',
                            formats: ['csv', 'excel'],
                        );
                }),
        ];
    }
}

class MyCustomWidget extends ValueWidget
{
    /**
     * Here you can configure your widget by calling whatever options are available for each widget
     */
    public function configure(NovaRequest $request): void
    {
        $this->icon('<svg>...</svg>');
        $this->title('Session Duration');
        $this->textColor('#f95738');
        $this->backgroundColor('#f957384f');
    }

    /**
     * This function is responsible for returning the actual data that will be shown on the widget,
     * each widget expects its own format, so please refer to the widget documentation 
     */
    public function value(Filters $filters): mixed
    {
        /**
         * $filters contain all the set values from the filters that were shown on the frontend. 
         * You can retrieve them and implement any custom logic you may have.
         */
        $filterValue = $filters->getFilterValue(LikesFilter::class);
        
        return 'example';
    }
}

$widget->layout(width: 2, height: 1, x: 0, y: 1);
$widget->minWidth(2);
$widget->minHeight(1);

use Illuminate\Http\Request;
use Laravel\Nova\Filters\BooleanFilter;

class ExampleFilter extends BooleanFilter
{
    public function apply(Request $request, $query, $value)
    {
        // this function is 

class SessionDuration extends ValueWidget
{
    public function value(Filters $filters): mixed
    {
        $filterA = $filters->getFilterValue(YourFilterClass::class);
        $filterB = $filters->getFilterValue(YourSecondFilterClass::class);
    }
}

class SessionDuration extends ValueWidget
{
    public function value(Filters $filters): mixed
    {
        $result = $filters->applyToQueryBuilder(User::query())->get();    
    }
}

use App\Models\User;
use MlSolutions\NovaDashboard\Card\View;
use MlSolutions\NovaDashboard\Downloads\DownloadResult;
use MlSolutions\NovaDashboard\Filters;
use Laravel\Nova\Http\Requests\NovaRequest;

$view->download(
    resolver: function (Filters $filters, NovaRequest $request, View $view, string $format) {
        return $filters
            ->applyToQueryBuilder(User::query())
            ->select('name', 'email', 'created_at');
    },
    label: 'Download Users',
    filename: 'users-report',
    formats: ['csv', 'excel'],
);

$view->download(
    resolver: function (Filters $filters, NovaRequest $request, View $view, string $format) {
        $rows = $filters
            ->applyToQueryBuilder(User::query())
            ->get()
            ->map(fn (User $user) => [
                'Name' => $user->name,
                'Email' => $user->email,
                'Joined At' => $user->created_at?->toDateString(),
            ]);

        return DownloadResult::make(
            columns: ['Name', 'Email', 'Joined At'],
            rows: $rows,
            filename: 'filtered-users',
        );
    },
);

$view->download(
    resolver: function (array $filterValues) {
        $dateRange = data_get($filterValues, App\Nova\Filters\CreateAtDateFilter::class . '.value');

        // Build your own query/service call from the active filter values.
        return [];
    },
);