Create a new ServiceProvider from the wizard by running the following command
php artisan make:provider ComposerServiceProvider
this will create a new ComposerServiceProvider.php file name in the application / Providers. In the download function of this newly created service provider, you can create functions with closure, as shown below:
view()->composer('partials.navbar', function ($view) { $view->with('genre', Genre::all()); });
here the view in question is navbar.blade.php in the view / partial files, which will have the named genre variable available through your application.
To make your code clean, what you can do is create a new function in ComposerServiceProvider and name it anything like, say, partialnav. Then follow these steps:
public function boot() { $this->partialNav(); }
If you want to separate it even more, you can create a new folder under the name of the application / Http, it allows you to use ViewCompoers. In this folder, create a new file called NavbarComposer.php with the following code:
class NavbarComposer { public function __construct() {
now go back to your partialposal function ComposerServiceProvider
public function partialNav() { view()->composer('partials.nav', 'App\Http\ViewComposers\NavbarComposer'); }
Remember to add this newly created ServiceProvider to your config / app.php
App\Providers\ComposerServiceProvider::class,