2.0.2 Study MenuBuilder.php (ok)

C:\xampp82\htdocs\testnet\app\MenuBuilder\MenuBuilder.php

<?php
namespace App\MenuBuilder;
class MenuBuilder
{
  private $menu = [];
  private function addRegularLink($id, $name, $slug, $icon, $iconType, $sequence = 0)
  {
    $hasIcon = ($icon === false || strlen($icon) === 0) ? false : true;
    if ($hasIcon) {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'hasIcon' => $hasIcon,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    } else {
      array_push($this->menu, [
        'id' => $id,
        'target' => 'link',
        'name' => $name,
        'slug' => $slug,
        'iconType' => $iconType,
        'sequence' => $sequence
      ]);
    }
  }
  public function addLink($id, $name, $slug, $icon = false, $iconType = 'coreui', $sequence = 0)
  {
    $this->addRegularLink($id, $name, $slug, $icon, $iconType, $sequence);
  }
  public function getResult()
  {
    // echo '<pre>';
    // var_export($this->menu);
    // echo '<pre>';
    return $this->menu;
  }
}
// array (
//   0 => 
//   array (
//     'id' => 3,
//     'target' => 'link',
//     'name' => 'Dashboard',
//     'slug' => '/',
//     'hasIcon' => true,
//     'iconType' => NULL,
//     'sequence' => 1,
//   ),
//   1 => 
//   array (
//     'id' => 18,
//     'target' => 'link',
//     'name' => 'Colors',
//     'slug' => '/colors',
//     'hasIcon' => true,
//     'iconType' => NULL,
//     'sequence' => 14,
//   ),
//   2 => 
//   array (
//     'id' => 20,
//     'target' => 'link',
//     'name' => 'Typography',
//     'slug' => '/typography',
//     'hasIcon' => true,
//     'iconType' => NULL,
//     'sequence' => 15,
//   ),
//   3 => 
//   array (
//     'id' => 66,
//     'target' => 'link',
//     'name' => 'Charts',
//     'slug' => '/charts',
//     'hasIcon' => true,
//     'iconType' => NULL,
//     'sequence' => 38,
//   ),
//   4 => 
//   array (
//     'id' => 84,
//     'target' => 'link',
//     'name' => 'Widgets',
//     'slug' => '/widgets',
//     'hasIcon' => true,
//     'iconType' => NULL,
//     'sequence' => 47,
//   ),
//   5 => 
//   array (
//     'id' => 99,
//     'target' => 'link',
//     'name' => 'Download CoreUI',
//     'slug' => 'https://coreui.io',
//     'hasIcon' => true,
//     'iconType' => NULL,
//     'sequence' => 54,
//   ),
//   6 => 
//   array (
//     'id' => 102,
//     'target' => 'link',
//     'name' => 'Try CoreUI PRO',
//     'slug' => 'https://coreui.io/pro/',
//     'hasIcon' => true,
//     'iconType' => NULL,
//     'sequence' => 55,
//   ),
// )

C:\xampp82\htdocs\testnet\app\Http\Menus\GetSidebarMenu.php

<?php
namespace App\Http\Menus;
use App\MenuBuilder\RenderFromDatabaseData;
use App\Models\Menus;
use Spatie\Permission\Models\Role;
class GetSidebarMenu implements MenuInterface
{
  private $menu;
  private function getMenuFromDB($role, $menuId)
  {
    $this->menu = Menus::join('menu_role', 'menus.id', '=', 'menu_role.menus_id')
      ->where('menus.menu_id', $menuId)
      ->where('menu_role.role_name', $role)
      ->orderBy('menus.sequence', 'asc')
      ->get();
  }
  public function get($role, $menuId = 2)
  {
    $this->getMenuFromDB($role, $menuId);
    $rfd = new RenderFromDatabaseData();
    // echo '<pre>';
    // var_export($this->menu);
    // echo '<pre>';
    return $rfd->render($this->menu);
  }
}
// \Illuminate\Database\Eloquent\Collection::__set_state(array(
//   'items' => 
//  array (
//    0 => 
//    \App\Models\Menus::__set_state(array(
//       'connection' => 'mysql',
//       'table' => 'menus',
//       'primaryKey' => 'id',
//       'keyType' => 'int',
//       'incrementing' => true,
//       'with' => 
//      array (
//      ),
//       'withCount' => 
//      array (
//      ),
//       'preventsLazyLoading' => false,
//       'perPage' => 15,
//       'exists' => true,
//       'wasRecentlyCreated' => false,
//       'escapeWhenCastingToString' => false,
//       'attributes' => 
//      array (
//        'id' => 3,
//        'name' => 'Dashboard',
//        'icon' => 'cil-speedometer',
//        'slug' => '/',
//        'target' => 'link',
//        'menu_id' => 1,
//        'parent_id' => NULL,
//        'sequence' => 1,
//        'created_at' => NULL,
//        'updated_at' => NULL,
//        'role_name' => 'admin',
//        'menus_id' => '1',
//      ),
//       'original' => 
//      array (
//        'id' => 3,
//        'name' => 'Dashboard',
//        'icon' => 'cil-speedometer',
//        'slug' => '/',
//        'target' => 'link',
//        'menu_id' => 1,
//        'parent_id' => NULL,
//        'sequence' => 1,
//        'created_at' => NULL,
//        'updated_at' => NULL,
//        'role_name' => 'admin',
//        'menus_id' => '1',
//      ),
//       'changes' => 
//      array (
//      ),
//       'casts' => 
//      array (
//      ),
//       'classCastCache' => 
//      array (
//      ),
//       'attributeCastCache' => 
//      array (
//      ),
//       'dates' => 
//      array (
//      ),
//       'dateFormat' => NULL,
//       'appends' => 
//      array (
//      ),
//       'dispatchesEvents' => 
//      array (
//      ),
//       'observables' => 
//      array (
//      ),
//       'relations' => 
//      array (
//      ),
//       'touches' => 
//      array (
//      ),
//       'timestamps' => true,
//       'hidden' => 
//      array (
//      ),
//       'visible' => 
//      array (
//      ),
//       'fillable' => 
//      array (
//      ),
//       'guarded' => 
//      array (
//        0 => '*',
//      ),
//    )),
//   //  ...
//  )
// ));

Last updated