Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active January 2, 2020 00:09
Show Gist options
  • Save nasrulhazim/89c094adab594bf45e6e1b56a6ade307 to your computer and use it in GitHub Desktop.
Save nasrulhazim/89c094adab594bf45e6e1b56a6ade307 to your computer and use it in GitHub Desktop.
Dashboard Menu for Laravel Nova
<?php
namespace NasrulHazim\DashboardMenu;
use Laravel\Nova\Card;
use Laravel\Nova\Nova;
use Illuminate\Support\Str;
class DashboardMenu extends Card
{
/**
* The width of the card (1/3, 1/2, or full).
*
* @var string
*/
public $width = 'full';
/**
* Get the component name for the element.
*
* @return string
*/
public function component()
{
return 'dashboard-menu';
}
public function getMenus()
{
return $this->withMeta([
'menus' => $this->populateMenus(),
]);
}
public function populateMenus()
{
$menus = [];
foreach (Nova::groupedResources(request()) as $group => $resources) {
$menu = [];
if(count($resources) > 0) {
$menu['id'] = (count(Nova::groups(request())) > 1) ? Str::kebab($group) : null;
$menu['group'] = (count(Nova::groups(request())) > 1) ? $group : null;
$menu['links'] = [];
foreach ($resources as $resource) {
if (! $resource::$displayInNavigation) {
continue;
}
$menu['links'][] = [
'id' => Str::kebab($resource::label()),
'name' => 'index',
'label' => $resource::label(),
'params' => [
'resourceName' => $resource::uriKey(),
]
];
}
$menus[] = $menu;
}
}
return $menus;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment