I have created a database driven menu using a Controller,
HomeController extends Controller which the menu is loaded in Controller's construct function.
HomeController.php
class HomeController extends Controller
{
public function __construct()
{
parent::__construct();
$this->middleware('auth');
}
public function index(){
$data['menu'] = $this->menu;
return view('home', $data);
}
}
Controller.php
public function __construct()
{
$this->user = Auth::user();
$menu = new Menu();
if($this->user != NULL && $this->user != ""){
$this->menu = $menu->getMenu($this->user->user_id);
}
}
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.
P/S: Using laravel 5.1
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