Laravel-adminlte: Add label data (Dynamic)

Created on 24 Nov 2017  路  17Comments  路  Source: jeroennoten/Laravel-AdminLTE

Can anyone explain how to add a label to the menu with a dynamic value coming from the database?

Most helpful comment

You can follow the 'Menu configuration at runtime' item on the README.

  1. Set 'menu' => [] inside config/adminlte.php
  2. Edit app\ProvidersAppServiceProvider.php boot method
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Events\Dispatcher;
use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
use \App\User;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Dispatcher $events)
    {
        $events->listen(BuildingMenu::class, function (BuildingMenu $event) {
            $event->menu->add('MAIN NAVIGATION');
            $event->menu->add([
                'text'        => 'Users',
                'url'         => 'admin/users',
                'icon'        => 'users',
                'label'       => User::count(),
                'label_color' => 'success',
            ]);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

All 17 comments

You can achieve such thing using a Listener to the BuildingMenu Event. For example, lets say you need to display the number of users registered on your app, you could do something like this:
```php

namespace App\Listeners;

use App\User;
use JeroenNoten\LaravelAdminLte\EventsBuildingMenu;

class BuildMenuListener {

/**
  * Create the event listener.
  *
  * @return void
  */
public function __construct()
{
     //
}

/**
  * Handle the event.
  *
  * @param  BuildingMenu  $event
  * @return void
  */
public function handle(BuildingMenu $event)
{
    $usersCount = User::count();

    $event->menu->add('MAIN NAVIGATION');
    $event->menu->add([
        'text'        => 'Users',
        'url'         => 'admin/users',
        'icon'        => 'users',
        'label'       => $usersCount,
        'label_color' => 'success'
    ]);

Thank u :D

You're welcome (: I think @jeroennoten can close this issue.

You can follow the 'Menu configuration at runtime' item on the README.

  1. Set 'menu' => [] inside config/adminlte.php
  2. Edit app\ProvidersAppServiceProvider.php boot method
<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Contracts\Events\Dispatcher;
use JeroenNoten\LaravelAdminLte\Events\BuildingMenu;
use \App\User;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot(Dispatcher $events)
    {
        $events->listen(BuildingMenu::class, function (BuildingMenu $event) {
            $event->menu->add('MAIN NAVIGATION');
            $event->menu->add([
                'text'        => 'Users',
                'url'         => 'admin/users',
                'icon'        => 'users',
                'label'       => User::count(),
                'label_color' => 'success',
            ]);
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

@guizoxxv how do i add a submenu? Can u make a sample too?

After much research and suffering ...

I found a solution to assemble your MENU and SUBMENU by the time of execution

Example:

// your model Menu
use App\Models\Menu;

class AppServiceProvider extends ServiceProvider{

    protected $listen = [
        Registered::class => [
            SendEmailVerificationNotification::class,
        ]
    ];
    public function boot(Dispatcher $events) {

       // ...... your sintaxe

        $events->listen(BuildingMenu::class, function (BuildingMenu $event) {

            $event->menu->add('MENU DE NAVEGA脟脙O');

            $menu = new Menu;
            $menus = $menu->withSubMenus();

            foreach($menus as $menu){

                $arrayMenu = array('text' => '', 'url' => '', 'icon' => '');

                if(count($menu->SubMenu) != NULL){
                    foreach($menu->SubMenu as $submenu){
                        $arrayMenu[] = array(
                            'text' => $submenu->name,
                            'url' => $submenu->url,
                            'icon' => $submenu->icon
                        );

                    };
                    $event->menu->add([
                        'text' => $menu->name,
                        'url' => $menu->url,
                        'icon' => $menu->icon,
                        'submenu' => $arrayMenu,
                    ]);
                }else{
                    $event->menu->add([
                        'text' => $menu->name,
                        'url' => $menu->url,
                        'icon' => $menu->icon,
                    ]);
                }
            }
        });
    }
}

Documentation of Laravel-AdminLTE:
https://github.com/jeroennoten/Laravel-AdminLTE#menu-configuration-at-runtime

Issue: #162

_Sorry for my bad english_

_Cleanup issue_

If the issue is omnipresent in the coming version 1.26.x, please create a new issue.

regarding to this solution, can we set the arrangement of the menu, lets say i setup my menu in the adminlte.php file, one of my menu item use a dynamic value (data for label) so i setup that menu item in the AppServiceProvider.php.

It will add my menu item at the bottom of the menu.
can i configure it to specific order instead of adding the item at the bottom.

Sorry if my question is not clear

Can I edit the menu in another place or only on AppServiceProvider? I would like to add or remove some itens in my controller...

Can I edit the menu in another place or only on AppServiceProvider? I would like to add or remove some itens in my controller...

if i not mistaken, only in the AppServiceProvider

There are several ways to accomplish this, you can modify it from the AppServiceProvider, any controller or simple use the BuildingMenu event.

Can I edit the menu in another place or only on AppServiceProvider? I would like to add or remove some itens in my controller...

if i not mistaken, only in the AppServiceProvider

Okay TKX Buddy! :)

There are several ways to accomplish this, you can modify it from the AppServiceProvider, any controller or simple use the BuildingMenu event.

Nice, so how can I instance some new BuildingMenu to add a new Menu? can you help me with this?

Hi @samuel-lujan , The issue #694 gives some explanation on how to listen the BuildingMenu form a Controller.

There are several ways to accomplish this, you can modify it from the AppServiceProvider, any controller or simple use the BuildingMenu event.

Nice, so how can I instance some new BuildingMenu to add a new Menu? can you help me with this?

@samuel-lujan you can read it here

Hi @samuel-lujan , The issue #694 gives some explanation on how to listen the BuildingMenu form a Controller.

Hey that's okay, Thanks! :)

There are several ways to accomplish this, you can modify it from the AppServiceProvider, any controller or simple use the BuildingMenu event.

Nice, so how can I instance some new BuildingMenu to add a new Menu? can you help me with this?

@samuel-lujan you can read it here

Nice I'll read it! thanks!

Was this page helpful?
0 / 5 - 0 ratings