Load dynamic content in navbar

1

I have a select in the navbar with the cities registered in the bank. For this I am using ajax to load them.

But I wanted a solution without needing to use ajax , but also without having to mount the query on each method that points to view . Would you like?

    
asked by anonymous 18.03.2018 / 13:13

1 answer

1

Yes, you do not need to use AJAX for this. Laravel has a function called view composer , where you can do exactly what you need.

I'll give you a brief summary of the two necessary files and leave the link to the documentation, where you can see the correct syntax.

First you have to create within the app/Http directory, the file that will contain the code that will be displayed in the view. I advise you to create a folder to stay more organized.

$cidades = Cidades::get();
$view->with('cidades', $cidades);

Then you need to create a file within app/Providers , which will be responsible for calling the previously created file.

view()->composer(
    ['nome-view-navbar'], 'App\Http\nome-pasta-criada\nome-arquivo-cidades'
);

Here is the link to the documentation: link

As you did not specify the version of your project, I put the link to the last one.

    
18.03.2018 / 13:20