Code Monkey home page Code Monkey logo

laravel-shop-menu's Introduction

Menu manager like Wordpress using Laravel and Nestable

See demo at: http://laravel-menu-builder.gopagoda.com/admin/menu Tutorial coming up at: http://maxoffsky.com

This application demonstrates usage of Nestable plugin jQuery plugin to provide the user with nice menu ordering experience without a page refresh: menu ordering in action

The menu controller is in app/controllers/Admin/MenuController.php The menu model is in app/models/Menu.php

A note on the data structure for the menu

The important columns of the "menus" table are:

  • id
  • parent_id
  • order

With these 3 fields we can build nested menus as many levels deep as you want. The Nestable plugin helps modify the values of these fields for the appropriate rows of data.

Use of recursion

The hard part that took me a looong time to build is a very small function inside of app/models/Menu.php:

public function buildMenu($menu, $parentid = 0) 
{ 
  $result = null;
  foreach ($menu as $item) 
    if ($item->parent_id == $parentid) { 
      $result .= "<li class='dd-item nested-list-item' data-order='{$item->order}' data-id='{$item->id}'>
      <div class='dd-handle nested-list-handle'>
        <span class='glyphicon glyphicon-move'></span>
      </div>
      <div class='nested-list-content'>{$item->label}
        <div class='pull-right'>
          <a href='".url("admin/menu/edit/{$item->id}")."'>Edit</a> |
          <a href='#' class='delete_toggle' rel='{$item->id}'>Delete</a>
        </div>
      </div>".$this->buildMenu($menu, $item->id) . "</li>"; 
    } 
  return $result ?  "\n<ol class=\"dd-list\">\n$result</ol>\n" : null; 
} 

This function uses recursion to display the menu to the user even if the menu is many many levels deep. This function alone can save you a bunch of time.

Installation instructions:

  • Download this repo
  • Set up a MySQL DB named 'shop-menu' and import install.sql file into it, make sure you edit credentials in app/config/database.php to match yours
  • Open up terminal and CD into the folder of this repo
  • Run "php artisan serve" to run the application
  • Open up the browser and navigate to "localhost:8000" to see it in action

Star this repo!

The more people star my repos - the more I will give back to the community.

Read more on my blog and follow on Twitter

I post tutorials all the time on my blog : http://maxoffsky.com, stay updated on my Twitter: http://twitter.com/msurguy

License

The Laravel menu manager is open-sourced software licensed under the MIT license

laravel-shop-menu's People

Contributors

msurguy avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

laravel-shop-menu's Issues

get items in front-end

Hi,

How do I get menu in front-end?

I mean when I use

$menus = TopMenu::orderby('order', 'asc')->get();
$topmenu = new TopMenu;
$topmenu = $topmenu->getHTML($menus);

I will get public function buildMenu($menu, $parentid = 0) from model which is include edit,delete buttons etc. at this point i only need to get menu items in order to show in navbar to users, what should i do?

Thanks.

error when running 'php artisan serve'

{"error":{"type":"Symfony\Component\Debug\Exception\FatalErrorException","me
ssage":"Class 'Fideloper\Proxy\ProxyServiceProvider' not found","file":"C:\xa
mpp\htdocs\menumaster\vendor\laravel\framework\src\Illuminate\Foundation
\ProviderRepository.php","line":158}}

Some remarks

Hey,
Thanks for the example code. I did it somewhat similar, but instead of dropping, just use a save button and serialize/save the entire form at once, and created a Twig macro for menu builder :)

I just have a few remarks/questions:

  • You escape all input, ie. e(Input::get('order')). But isn't this unneeded because of Laravels ORM? Isn't it more logical to escape when displaying data?
    • You use dropCallback, which isn't in the linked version of Nestable. What fork do you use/recommend, as the original project doesn't seem to be maintained?
    • Instead of buildMenu function directly, you could also create a method to build a tree, and use that to create your menu. This would make the buildMenu function a tiny bit simpler and you could use the same tree to build the front-end menu. (Just a suggestion)
    • Isn't it best practice to create links to actions (or named routes), and not just (url('admin/menu/etc')). Doesn't matter that much, but when you are creating a tutorial, might as well use best practices.

facing compilation problem in ubuntu

PHP Warning: require(/home/cavisson/work/laravel/laravel-shop-menu-master/bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in /home/cavisson/work/laravel/laravel-shop-menu-master/bootstrap/autoload.php on line 17
PHP Fatal error: require(): Failed opening required '/home/cavisson/work/laravel/laravel-shop-menu-master/bootstrap/../vendor/autoload.php' (include_path='.:/usr/share/php') in /home/cavisson/work/laravel/laravel-shop-menu-master/bootstrap/autoload.php on line 17

Rendering issue

This is what I get in return

121

code

model

public function buildMenu($menu, $parentid = 0) 
	{ 
	  $result = null;
	  foreach($menu as $item) 
	    if ($item->parent_id == $parentid) { 
	      $result .= "<li class='dd-item nested-list-item' data-order='{$item->order}' data-id='{$item->id}'>
	      <div class='dd-handle nested-list-handle'>
            <i class='fas fa-arrows-alt'></i>
	      </div>
	      <div class='nested-list-content'>{$item->title}
	        <div class='float-right'>
	          <a href='/admin/menusnav/{$item->id}'>Edit</a> |
	          <a href='#' class='delete_toggle text-danger' rel='{$item->id}'>Delete</a>
	        </div>
	      </div>".$this->buildMenu($menu, $item->id) . "</li>"; 
	    } 
	  return $result ?  "\n<ol class=\"dd-list\">\n$result</ol>\n" : null; 
	}
	// Getter for the HTML menu builder
	public function getHTML($items)
	{
		return $this->buildMenu($items);
  }

controller

public function index() {
                 $categories = Category::orderby('title', 'asc')->get();
		//navbar
		$navbars = NavbarMenu::orderby('order', 'asc')->get();

		$navbar = new NavbarMenu;
               $navbar = $navbar->getHTML($navbars);
        
        return view('admin.menus.index', compact('navbars', 'navbar'));
    }

and my blade

{!! $navbar !!}

Any idea what might cause the problem?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.