Load dynamic content in navbar

Asked

Viewed 76 times

1

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

But I wanted a solution without having to use ajax, but also without having to assemble the query in each method that points to the view. Would have as?

1 answer

1


Yes, no need to use AJAX for that. The Laravel has a function called view composer, where it is possible to do exactly what you need.

I will give a brief summary of the two files needed and leave the link to the documentation, where you can consult the correct syntax.

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

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

Then you need to create a file inside app/Providers, who will be responsible for calling the file created earlier.

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

Here is the link to the documentation: https://laravel.com/docs/5.6/views#view-Composers

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

  • Bro was that. Thank you. I will replace all ajax by it!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.