PHP code example of hayderhatem / filament-sub-navigation

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

    

hayderhatem / filament-sub-navigation example snippets




namespace App\Filament\Resources;

use Filament\Resources\Resource;
use HayderHatem\FilamentSubNavigation\Concerns\HasBadgeSubNavigation;

class UserResource extends Resource
{
    use HasBadgeSubNavigation;

    // ... your existing resource code
}

public static function getNavigationItems(): array
{
    return [
        static::createBadgeNavigation(
            label: 'Users',
            icon: 'heroicon-o-users',
            url: static::getUrl('index'),
            isActiveWhen: fn (): bool => request()->routeIs([
                'filament.admin.resources.users.*'
            ]),
            badge: static::getNavigationBadge(),
            subItems: static::getSubNavigationItems()
        ),
    ];
}

public static function getSubNavigationItems(): array
{
    return [
        'users' => [ // This key should match your navigation label (lowercased, alphanumeric only)
            [
                'label' => 'All Users',
                'description' => 'View and manage all users',
                'url' => static::getUrl('index'),
            ],
            [
                'label' => 'Create User',
                'description' => 'Add a new user to the system',
                'url' => static::getUrl('create'),
            ],
            [
                'label' => 'User Categories',
                'description' => 'Manage user categories',
                'url' => route('filament.admin.resources.categories.index'),
            ],
            [
                'label' => 'User Reports',
                'description' => 'View detailed user analytics',
                'url' => route('filament.admin.resources.reports.index'),
            ],
            [
                'label' => 'User Settings',
                'description' => 'Configure user preferences',
                'url' => route('filament.admin.resources.settings.index'),
            ],
        ],
    ];
}



namespace App\Providers\Filament;

use Filament\Http\Middleware\Authenticate;
use Filament\Http\Middleware\DisableBladeIconComponents;
use Filament\Http\Middleware\DispatchServingFilamentEvent;
use Filament\Panel;
use Filament\PanelProvider;
use Filament\Support\Colors\Color;
use Filament\Widgets;
use Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse;
use Illuminate\Cookie\Middleware\EncryptCookies;
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Routing\Middleware\SubstituteBindings;
use Illuminate\Session\Middleware\AuthenticateSession;
use Illuminate\Session\Middleware\StartSession;
use Illuminate\View\Middleware\ShareErrorsFromSession;

class AdminPanelProvider extends PanelProvider
{
    public function panel(Panel $panel): Panel
    {
        return $panel
            ->default()
            ->id('admin')
            ->path('admin')
            ->login()
            ->colors([
                'primary' => Color::Amber,
            ])
            ->discoverResources(in: app_path('Filament/Resources'), for: 'App\\Filament\\Resources')
            ->discoverPages(in: app_path('Filament/Pages'), for: 'App\\Filament\\Pages')
            ->pages([
                Pages\Dashboard::class,
            ])
            ->discoverWidgets(in: app_path('Filament/Widgets'), for: 'App\\Filament\\Widgets')
            ->widgets([
                Widgets\AccountWidget::class,
                Widgets\FilamentInfoWidget::class,
            ])
            ->middleware([
                EncryptCookies::class,
                AddQueuedCookiesToResponse::class,
                StartSession::class,
                AuthenticateSession::class,
                ShareErrorsFromSession::class,
                VerifyCsrfToken::class,
                SubstituteBindings::class,
                DisableBladeIconComponents::class,
                DispatchServingFilamentEvent::class,
            ])
            ->authMiddleware([
                Authenticate::class,
            ])
            ->renderHook(
                'panels::body.end',
                fn (): string => $this->getSubNavigationScript()
            );
    }

    protected function getSubNavigationScript(): string
    {
        $script = '';
        
        // Register sub-navigation data for each Resource that uses the trait
        $resources = [
            \App\Filament\Resources\UserResource::class,
            // Add other resources that use HasBadgeSubNavigation here
        ];

        foreach ($resources as $resourceClass) {
            if (
                class_exists($resourceClass) &&
                method_exists($resourceClass, 'getSubNavigationItems')
            ) {
                $subNavItems = $resourceClass::getSubNavigationItems();
                
                if (!empty($subNavItems)) {
                    foreach ($subNavItems as $id => $items) {
                        $itemsJson = json_encode($items);
                        $script .= "window.registerSubNavigation('{$id}', {$itemsJson});";
                    }
                }
            }
        }

        return $script ? '<script>' . $script . '</script>' : '';
    }
}

// In ProductResource.php
class ProductResource extends Resource
{
    use HasBadgeSubNavigation;

    public static function getSubNavigationItems(): array
    {
        return [
            'products' => [
                [
                    'label' => 'All Products',
                    'url' => static::getUrl('index'),
                ],
                [
                    'label' => 'Add Product',
                    'url' => static::getUrl('create'),
                ],
                [
                    'label' => 'Categories',
                    'url' => route('filament.admin.resources.categories.index'),
                ],
            ],
        ];
    }
}

// In OrderResource.php
class OrderResource extends Resource
{
    use HasBadgeSubNavigation;

    public static function getSubNavigationItems(): array
    {
        return [
            'orders' => [
                [
                    'label' => 'All Orders',
                    'url' => static::getUrl('index'),
                ],
                [
                    'label' => 'Pending Orders',
                    'url' => static::getUrl('index') . '?status=pending',
                ],
                [
                    'label' => 'Completed Orders',
                    'url' => static::getUrl('index') . '?status=completed',
                ],
            ],
        ];
    }
}

[
    'label' => 'Item Label',           // Required: Display text
    'description' => 'Item description', // Optional: Subtitle text
    'url' => '/admin/some-path',       // Required: Target URL
    'icon' => 'heroicon-o-star',       // Optional: Icon (future feature)
    'badge' => '5',                    // Optional: Badge count (future feature)
]