F
Filament16mo ago
ntpscrt

How to generate NavigationItem from DB ?

I would like to make custom Navigation by using Eloquent model, It works in V2 with these code.
AppServiceProvider.php
...
public function boot()
{
Filament::serving(function () {
$menus = Menu::all()
->map(function ($menu) {
return NavigationItem::make($menu->title)
->url($menu->filamentResourceUrl)
->icon($menu->filamentResourceIcon)
->isActiveWhen(fn (): bool => request('menu') == $menu->id)
->group($menu->filamentResourceMenuGroupLabel);
// ->sort(3);
});

Filament::registerNavigationItems([
...$menus->toArray(),
]);
});
}
...
AppServiceProvider.php
...
public function boot()
{
Filament::serving(function () {
$menus = Menu::all()
->map(function ($menu) {
return NavigationItem::make($menu->title)
->url($menu->filamentResourceUrl)
->icon($menu->filamentResourceIcon)
->isActiveWhen(fn (): bool => request('menu') == $menu->id)
->group($menu->filamentResourceMenuGroupLabel);
// ->sort(3);
});

Filament::registerNavigationItems([
...$menus->toArray(),
]);
});
}
...
In V3, I can't call any db query here and i have no idea where it should be.
Call to a member function connection() on null
Call to a member function connection() on null
https://flareapp.io/share/DPybYXD7#context-context-versions
class AdminPanelProvider extends PanelProvider
{

public function panel(Panel $panel): Panel
{
return $panel
...
->navigationItems($this->populateNavigationItems())
}



private function populateNavigationItems()
{
return Menu::all()
->map(function ($menu) {

return NavigationItem::make($menu->title)
->url(fn () => PostResource::getUrl(name: 'index', parameters: ['menu_id' => $menu->id]))
->icon('heroicon-o-newspaper')
->group('ข่าวสาร')
->sort(3);
})
->filter(fn ($menu) => !is_null($menu))
->toArray();
}
}
class AdminPanelProvider extends PanelProvider
{

public function panel(Panel $panel): Panel
{
return $panel
...
->navigationItems($this->populateNavigationItems())
}



private function populateNavigationItems()
{
return Menu::all()
->map(function ($menu) {

return NavigationItem::make($menu->title)
->url(fn () => PostResource::getUrl(name: 'index', parameters: ['menu_id' => $menu->id]))
->icon('heroicon-o-newspaper')
->group('ข่าวสาร')
->sort(3);
})
->filter(fn ($menu) => !is_null($menu))
->toArray();
}
}
Any ideas ?
1 Reply
Want results from more Discord servers?
Add your server