PHP code example of eightynine / filament-reports

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

    

eightynine / filament-reports example snippets



use EightyNine\Reports\ReportsPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        ->default()
        ->id('demo')
        ->path('demo')
        ...
        ->plugins([
            ReportsPlugin::make()
        ]);
}


namespace App\Filament\Reports;

use EightyNine\Reports\Report;
use Filament\Schemas\Schema;

class UserReport extends Report
{
    public ?string $heading = "Report";

    // public ?string $subHeading = "A report";

    public function header(Schema $schema): Schema
    {
        return $schema->components([
            // Add header components here
        ]);
    }

    public function body(Schema $schema): Schema
    {
        return $schema->components([
            // Add body components here
        ]);
    }

    public function footer(Schema $schema): Schema
    {
        return $schema->components([
            // Add footer components here
        ]);
    }

    public function filterForm(Schema $schema): Schema
    {
        return $schema->components([
            // Add filter form components here
        ]);
    }
}


// AdminPanelProvider.php
use EightyNine\Reports\ReportsPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        ->id('admin')
        ->plugins([
            ReportsPlugin::make()
                ->reports([
                    \App\Filament\Reports\UserReport::class,
                    \App\Filament\Reports\OrderReport::class,
                    \App\Filament\Reports\FinancialReport::class,
                ])
        ]);
}

// CustomerPanelProvider.php
public function panel(Panel $panel): Panel
{
    return $panel
        ->id('customer')
        ->plugins([
            ReportsPlugin::make()
                ->excludeReports([
                    \App\Filament\Reports\AdminReport::class,
                    \App\Filament\Reports\SystemReport::class,
                ])
        ]);
}

// ManagerPanelProvider.php
public function panel(Panel $panel): Panel
{
    return $panel
        ->id('manager')
        ->plugins([
            ReportsPlugin::make()
                ->filterReports(function (string $reportClass, Panel $panel) {
                    $report = app($reportClass);
                    // Only show sales and finance reports to managers
                    return in_array($report->group, ['sales', 'finance']);
                })
        ]);
}



namespace App\Filament\Reports;

use EightyNine\Reports\Report;

class SalesReport extends Report
{
    public ?string $heading = "Sales Report";
    
    // This report will only appear in admin and manager panels
    protected array $panels = ['admin', 'manager'];
    
    // Or use the method approach
    public function panels(): array
    {
        return ['admin', 'manager'];
    }
    
    // ... rest of your report implementation
}

// config/filament-reports.php
return [
    'reports_directory' => app_path('Filament/Reports'),
    'reports_namespace' => 'App\\Filament\\Reports',
    
    // Panel-specific configurations
    'panel_reports' => [
        'admin' => [
            'directory' => app_path('Filament/AdminReports'),
            'namespace' => 'App\\Filament\\AdminReports',
        ],
        'customer' => [
            'directory' => app_path('Filament/CustomerReports'),
            'namespace' => 'App\\Filament\\CustomerReports',
        ],
    ],
];

public function panel(Panel $panel): Panel
{
    return $panel
        ->id('admin')
        ->plugins([
            ReportsPlugin::make()
                ->reports([
                    UserReport::class,
                    OrderReport::class,
                    SalesReport::class,
                ])
                ->excludeReports([
                    // Conditionally exclude based on user permissions
                    auth()->user()->can('view-sensitive-reports') ? [] : [SensitiveReport::class]
                ])
                ->filterReports(function (string $reportClass, Panel $panel) {
                    $report = app($reportClass);
                    
                    // Additional business logic
                    if ($report instanceof TimeSensitiveReport) {
                        return $report->isCurrentlyAvailable();
                    }
                    
                    return true;
                })
        ]);
}


    public function header(Schema $schema): Schema
    {
        return $schema->components([
            Header\Layout\HeaderRow::make()
            ->components([
                Header\Layout\HeaderColumn::make()
                    ->schema([
                        Text::make("User registration report")
                            ->title()
                            ->primary(),
                        Text::make("A user registration report")
                            ->subtitle(),
                    ]),
                Header\Layout\HeaderColumn::make()
                    ->schema([
                        Image::make($imagePath),
                    ])
                    ->alignRight(),
            ]),
        ]);
    }



    public function body(Schema $schema): Schema
    {
        return $schema->components([
            Body\Layout\BodyColumn::make()
                ->schema([
                    Body\Table::make()
                        ->columns([
                            EightyNine\Reports\Components\Body\TextColumn::make("name"),
                            EightyNine\Reports\Components\Body\TextColumn::make("age")
                                ->numeric()
                        ])
                        ->data(
                            fn(?array $filters) => collect([
                                [ "name" => "One",   "age" => 5 ],
                                [ "name" => "Two",   "age" => 5 ],
                                [ "name" => "Three", "age" => 5 ],
                                [ "name" => "Four",  "age" => 5 ],
                            ])
                        ),
                        VerticalSpace::make(),
                        Body\Table::make()
                            ->data(
                                fn(?array $filters) => $this->verificationSummary($filters)
                            ),
                    ]),
            ]);
    }




    public function footer(Schema $schema): Schema
    {
        return $schema->components([
            Footer\Layout\FooterRow::make()
                ->schema([
                    Footer\Layout\FooterColumn::make()
                        ->schema([
                            Text::make("Footer title")
                                ->title()
                                ->primary(),
                            Text::make("Footer subtitle")
                                ->subtitle(),
                        ]),
                    Footer\Layout\FooterColumn::make()
                        ->schema([
                            Text::make("Generated on: " . now()->format('Y-m-d H:i:s')),
                        ])
                        ->alignRight(),
                ]),
        ]);
    }


public function filterForm(Schema $schema): Schema
{
    return $schema->components([
        Input::make('search')
            ->placeholder('Search')
            ->autofocus()
            ->iconLeft('heroicon-o-search'),
        Select::make('status')
            ->placeholder('Status')
                ->options([
                    'active' => 'Active',
                    'inactive' => 'Inactive',
                ]),
        ]);
}

    use EightyNine\Reports\Components\Body\TextColumn;

    Body\Table::make()
        ->columns([
            TextColumn::make("location")
                ->groupRows(),
            TextColumn::make("name"),
            TextColumn::make("age")
                ->numeric()
        ])
        ->data(
            fn(?array $filters) => collect([
                ["location"=>"New York", "name" => "One",   "age" => 5 ],
                ["location"=>"New York", "name" => "Two",   "age" => 5 ],
                ["location"=>"Florida", "name" => "Three", "age" => 5 ],
                ["location"=>"New York", "name" => "Four",  "age" => 5 ],
            ])->orderBy('location')
        ),

use EightyNine\Reports\Components\Header;
use EightyNine\Reports\Components\Body;
use EightyNine\Reports\Components\Footer;
use Filament\Forms\Form;

class MyReport extends Report
{
    public function header(Header $header): Header
    {
        return $header->schema([
            Text::make('My Report Title'),
        ]);
    }

    public function body(Body $body): Body
    {
        return $body->schema([
            Text::make('Report content'),
        ]);
    }

    public function footer(Footer $footer): Footer
    {
        return $footer->schema([
            Text::make('Footer content'),
        ]);
    }

    public function filterForm(Form $form): Form
    {
        return $form->schema([
            TextInput::make('search'),
        ]);
    }
}

use Filament\Schemas\Schema;

class MyReport extends Report
{
    public function header(Schema $schema): Schema
    {
        return $schema->components([
            Text::make('My Report Title'),
        ]);
    }

    public function body(Schema $schema): Schema
    {
        return $schema->components([
            Text::make('Report content'),
        ]);
    }

    public function footer(Schema $schema): Schema
    {
        return $schema->components([
            Text::make('Footer content'),
        ]);
    }

    public function filterForm(Schema $schema): Schema
    {
        return $schema->components([
            TextInput::make('search'),
        ]);
    }
}

// Components still work the same way
Text::make('Hello World')
    ->color('primary')
    ->size('lg'),

Image::make('/path/to/image.jpg')
    ->width(200)
    ->height(150),

public function filterForm(Schema $schema): Schema
{
    return $schema->components([
        Section::make('Filters')
            ->schema([
                TextInput::make('search')
                    ->label('Search'),
                DatePicker::make('date_from')
                    ->label('From Date'),
                DatePicker::make('date_to')
                    ->label('To Date'),
            ]),
    ]);
}
bash
php artisan vendor:publish --provider="EightyNine\Reports\ReportsServiceProvider" --tag="reports-config"
bash
php artisan make:filament-report UsersReport