I want to do right. Conceived that you can create pages on the site through the admin panel. Created tables pages in the database. Page model and PageController resource PageController . The route is also configured.

In the PageController controller for displaying the page in the show method, I return the view and data for display. It's all clear. Request a page - received.

When I write the admin panel, I’ll use the remaining methods of the PageController controller: store , destroy and others, including index - to display a list of all pages. Thus, one table in the database is one model, one route-resource, one controller.

However, the site has a menu rendered as a separate presentation. The question arises - how to dynamically generate a menu. The index method of the PageController controller PageController busy (for the admin; see above). In addition, my menu is a separate presentation, the child of the parent. There you can’t simply transfer the data there - you will have to send it to each parent species, and this is a bunch of unnecessary code, because the menu is displayed on every page of the site.

That's actually the question - how to get out? How to get data to display this menu? So to say the True Laravel Way.

  • First, the controller can be divided into two AdminPageController and simply PageController , so as not to interfere with sections. Then you are released new action games. Secondly, once the menu is generated when a page is displayed, that is, in the show method, then the generation should somehow occur there, with the help of some service. - u_mulder
  • Well, how to transfer data? My menu is in 10 types, for example, it is connected. Not directly - through inheritance. I deduce for example some kind of display for, say, a blog post. So what? To me in the controller there to search and transfer the list of pages. And then in another controller to output, for example, a product - also search for pages and output? - n.osennij
  • @u_mulder is there some kind of approach? - n.osennij

2 answers 2

For these purposes, use View Composers . You can assign data to certain types, etc. Very comfortably.

  • You can indicate that this is the answer to the question. - Ilya Zelenko
  • @ IlyaZelenko Tomorrow I can only. There is a limit - n.osennij

Recently did the same.

I did this:

  1. Each menu item stores the parent id. For those who do not have a parent, the parent id is null.
  2. We build an array in the controller as follows: we put the oldest ones in the beginning, then their children (not grandchildren), and so on.

  3. Further in a single pass, simply insert the elements into each other. The design of the array allows. You can insert into domdocumet with php or directly into a page with ajax.

Thus, we will not do many actions on the front end and prescribe the menu.

A table with pages through a foreign key is connected to the menu item. Those. there will be 2 NavigationElementsController and PagesController controllers.

  • the question is not how to build this menu, but how to throw it into any view - n.osennij