Database driven menu that can be loaded in view

The problem : I have created a database driven menu using a Controller. How can I, call the function straight at the view level because right now, even though the menu is loaded in the constructor, I will still need to pass the menu to the view which makes things a bit redundant.

Solution :

Generate a new ServiceProvider from artisan by following command

this will create a new file name ComposerServiceProvider.php under app/Providers. In the boot function of this newly created service provider you can create functions with closure like following :

here the view in question is navbar.blade.php under view/partials which will have a variable named genre available through out your app.

To make your code cleaner what you can do is create a new function in the ComposerServiceProvider and name it anything lets say partialnav. Then will do the following :

If you want to separate it even more you can create a new folder under app/Http name it lets say ViewCompoers, Under this folder create a new file named NavbarComposer.php with the following code :

now back to your ComposerServiceProvider’s partialnav function

Don’t forget to add this newly created ServiceProvider in your config/app.php