PHP code example of vaslv / filament-topbar-menu

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

    

vaslv / filament-topbar-menu example snippets


use Vaslv\FilamentTopbarMenu\TopbarMenuPlugin;

public function panel(Panel $panel): Panel
{
    return $panel
        // ...
        ->plugin(TopbarMenuPlugin::make());
}

TopbarMenuPlugin::make()
    // Hide the management resource on this panel (e.g. a public panel
    // that should only display the menu):
    ->resource(false)

    // Put the resource into a navigation group / position:
    ->resourceNavigationGroup('Settings')
    ->resourceNavigationSort(10)

    // Render the menu at a different panel render hook:
    ->renderHook(\Filament\View\PanelsRenderHook::TOPBAR_START)

    // Drop the mobile copy of the menu (see "Responsive behavior"):
    ->mobile(false)

    // Put the mobile copy in the panel's own sidebar, below its navigation,
    // instead of in a slide-in panel of its own:
    ->mobileSidebar()

    // Render the mobile copy at a different panel render hook:
    ->mobileRenderHook(\Filament\View\PanelsRenderHook::SIDEBAR_NAV_START)

use Vaslv\FilamentTopbarMenu\Models\TopbarMenuItem;

TopbarMenuItem::create([
    'label' => 'Grafana',
    'type' => 'url',
    'url' => 'https://grafana.example.com',
    'target' => '_blank',
]);

TopbarMenuItem::create([
    'label' => 'Orders',
    'type' => 'route',
    'route' => 'filament.admin.resources.orders.index',
]);

TopbarMenuItem::create([
    'label' => 'Monthly report',
    'type' => 'route',
    'route' => 'reports.show',
    'route_parameters' => ['report' => 'monthly'],
]);

$services = TopbarMenuItem::create([
    'label' => 'Services',
    'type' => 'url', // a group with children is a toggle; its own url is not used
]);

TopbarMenuItem::create([
    'label' => 'Analytics',
    'type' => 'url',
    'url' => 'https://analytics.example.com',
    'parent_id' => $services->id,
]);

TopbarMenuItem::create([
    'label' => 'Admin dashboard',
    'type' => 'route',
    'route' => 'filament.admin.pages.dashboard',
    'parent_id' => $services->id,
]);

['auth' => true]                    // authenticated users only
['guest' => true]                   // guests only
['roles' => ['admin', 'ops']]       // users with any of these roles
                                    // (ee "Hiding each service's own link")

// config/filament-topbar-menu.php

return [
    'table_name' => 'filament_topbar_menu_items',
    // Database connection for the menu table. Unset it resolves to null and
    // follows the app's default connection (the one set by DB_CONNECTION), so
    // no separate menu database is assumed and existing installs keep working
    // unchanged after an update. Set the optional FILAMENT_TOPBAR_MENU_DB_CONNECTION
    // env variable to a dedicated connection to keep the menu in a separate,
    // possibly shared, database (see "Shared menu across projects" below).
    'connection' => env('FILAMENT_TOPBAR_MENU_DB_CONNECTION') ?: null,
    // A PREFIX, not the whole key: the connection and the table the menu was
    // read from are appended, so changing either cannot serve the previous
    // database's menu until the entry expires.
    'cache_key' => 'filament-topbar-menu.items',
    'cache_ttl' => 3600,
    'enable_favicons' => true,
    // Timeout of a single outbound request.
    'favicon_request_timeout' => 5,
    // Wall-clock budget for resolving ONE item, across every request its search
    // makes — the page, the icons it declares, the conventional locations, and
    // the redirects any of them follow. Running out is reported as "no favicon
    // found".
    'favicon_resolution_timeout' => 20,
    // Cap on an EMBEDDED favicon, applied to the decoded image (see "Favicons").
    'favicon_max_bytes' => 65536,
    // The resource's read-only View page: a `{record}` route, a View row
    // action, and create/edit saves landing on that page (see "View page"
    // above). Set FILAMENT_TOPBAR_MENU_VIEW_PAGE=false for a plain
    // list/create/edit resource.
    'enable_view_page' => env('FILAMENT_TOPBAR_MENU_VIEW_PAGE', true),
    // Default value of the target field for new items. The per-item choice
    // ("Same tab" / "New tab") always wins at render time; this is only a default.
    'open_external_links_in_new_tab' => true,
];

// config/database.php
'connections' => [
    'menu' => [
        'driver' => 'mysql',
        // ...credentials for the shared menu database...
    ],
],

// .env (in every app that shares the menu)
FILAMENT_TOPBAR_MENU_DB_CONNECTION=menu

foreach ([
    'Accounts'  => 'https://accounts.example.com',
    'Billing'   => 'https://billing.example.com',
    'Analytics' => 'https://analytics.example.com',
] as $label => $url) {
    TopbarMenuItem::create([
        'label' => $label,
        'type' => TopbarMenuItem::TYPE_URL,
        'url' => $url,
        'visibility' => ['hide_on_current_domain' => true],
    ]);
}

use Vaslv\FilamentTopbarMenu\Facades\TopbarMenu;

TopbarMenu::flushCache();
bash
php artisan migrate
bash
php artisan vendor:publish --tag=filament-topbar-menu-migrations
php artisan vendor:publish --tag=filament-topbar-menu-config
bash
php artisan db:seed --class="Vaslv\FilamentTopbarMenu\Database\Seeders\TopbarMenuSeeder"
bash
php artisan filament-topbar-menu:create-table
bash
php artisan vendor:publish --tag=filament-topbar-menu-views
bash
php artisan vendor:publish --tag=filament-topbar-menu-translations