From f76c2e478f050ec6aa97ce7c4ea07eca8d94c1bf Mon Sep 17 00:00:00 2001 From: Kiko Seijo Date: Tue, 23 Apr 2019 21:37:03 +0200 Subject: [PATCH 1/2] With this helper function and a blade, have reduce the database calls into half. --- README.md | 32 +++++++++++++++++------------ resources/views/menu-item.blade.php | 20 ++++++++++++++++++ src/Http/helpers.php | 11 ++++++++++ 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 resources/views/menu-item.blade.php diff --git a/README.md b/README.md index 44f8cb0..f02f285 100644 --- a/README.md +++ b/README.md @@ -4,10 +4,8 @@ This tool allows you to create menus in Laravel Nova menu builder Home - Menu Builder Items - ## Installation You can install the package in to a Laravel app that uses [Nova](https://nova.laravel.com) via composer: @@ -23,7 +21,6 @@ php artisan vendor:publish --tag=menu-builder-migration php artisan migrate ``` - ## Usage Next up, you must register the tool with Nova. This is typically done in the tools method of the NovaServiceProvider. @@ -46,9 +43,9 @@ public function tools() There are three helpers built in for your blades -* **menu_builder('slug')**. +- **menu_builder('slug')**. - Creates an html menu for given slug. Extra options are not required. By default tags are `ul` and `li`, and without html classes. + Creates an html menu for given slug. Extra options are not required. By default tags are `ul` and `li`, and without html classes. ```php {!! menu_builder('main') !!} @@ -58,28 +55,38 @@ There are three helpers built in for your blades {!! menu_builder('main', 'parent-class', 'child-class', 'dl', 'dd') !!} ``` -* **menu_name('slug')**. +- **menu_name('slug')**. - Returns the name of the menu for a given slug. + Returns the name of the menu for a given slug. ```php {{ menu_name('main') }} ``` -* **menu_json('slug')**. +- **menu_json('slug')**. - Returns a json with all items for given slug. + Returns a json with all items for given slug. ```php {!! menu_json('main') !!} ``` +## Example using blade. + +```blade +@foreach (getMenuBySlug('header')->items as $menuItem) + @include('menu-builder::menu-item', [ + 'menu' => $menuItem, + 'active_top_class' => 'active', + 'active_child_class' => 'active', + ]) +@endforeach +``` ## Localization Set your translations in the corresponding xx.json file located in /resources/lang/vendor/nova - ```json "Menu Builder": "Menu Builder", "Menu Items": "Menu Items", @@ -115,7 +122,6 @@ Set your translations in the corresponding xx.json file located in /resources/la "Disabled": "Disabled" ``` - ## Changelog Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently. @@ -136,8 +142,8 @@ Our address is: Infinety - Calle Comedias, 8 Floor 3, Suite 5 46003 Valencia (Sp ## Credits -- [Eric Lagarda](https://github.com/Krato) -- [Ralph Huwiler (vue-nestable)](https://github.com/rhwilr/vue-nestable) +- [Eric Lagarda](https://github.com/Krato) +- [Ralph Huwiler (vue-nestable)](https://github.com/rhwilr/vue-nestable) ## License diff --git a/resources/views/menu-item.blade.php b/resources/views/menu-item.blade.php new file mode 100644 index 0000000..8f52bd6 --- /dev/null +++ b/resources/views/menu-item.blade.php @@ -0,0 +1,20 @@ +@php + + $menu_class = $active_top_class ?? 'menu-item-has-children'; + $child_class = $active_child_class ?? ' current-menu-item'; + + $has_children = count($menu->children) > 0 ? $menu_class : false; + $is_active = set_active($menu->type == 'route' ? $menu->route : $menu->link, $child_class); + +@endphp +
  • + {{ $menu->name }} + @if($has_children) + + @endif + {{ $slot ?? '' }} +
  • diff --git a/src/Http/helpers.php b/src/Http/helpers.php index 9b9bfdd..4189897 100644 --- a/src/Http/helpers.php +++ b/src/Http/helpers.php @@ -2,6 +2,17 @@ use Infinety\MenuBuilder\Http\Models\Menu; +if (!function_exists('getMenuBySlug')) { + function getMenuBySlug($slug) + { + return Menu::whereHas('items', function($query){ + $query->with('children'); + $query->where('enabled',1); + $query->orderBy('order', 'asc'); + })->whereSlug($slug)->first(); + } +} + if (!function_exists('menu_builder')) { /** From 6642bfb4f43807bbd8a50897464c1500f9fc8169 Mon Sep 17 00:00:00 2001 From: Kiko Seijo Date: Tue, 23 Apr 2019 22:02:14 +0200 Subject: [PATCH 2/2] Fix submenu on top level. --- README.md | 2 +- src/Http/helpers.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index f02f285..e1b97f3 100644 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ There are three helpers built in for your blades ## Example using blade. ```blade -@foreach (getMenuBySlug('header')->items as $menuItem) +@foreach (getMenuBySlug('header')->parentItems as $menuItem) @include('menu-builder::menu-item', [ 'menu' => $menuItem, 'active_top_class' => 'active', diff --git a/src/Http/helpers.php b/src/Http/helpers.php index 4189897..2886d52 100644 --- a/src/Http/helpers.php +++ b/src/Http/helpers.php @@ -5,10 +5,10 @@ if (!function_exists('getMenuBySlug')) { function getMenuBySlug($slug) { - return Menu::whereHas('items', function($query){ - $query->with('children'); - $query->where('enabled',1); - $query->orderBy('order', 'asc'); + return Menu::whereHas('parentItems', function ($query) { + $query->with('children') + ->where('enabled', 1) + ->orderBy('order', 'asc'); })->whereSlug($slug)->first(); } }