Tuesday, February 28, 2017

How to efficiently create and store navigations in Laravel 5?

I'm coding up a simple CMS with Laravel 5.4.

I'm looking to have a wordpress-like sub-menu navigation management. I coded the pages in a multi-tier way, so that I can have multiple levels of pages (children etc.) In MySql, I have a 'Pages' table with all sorts of information, including a parent_id.

I'm looking to create a 'navigation generator' which would allow content writers to create multiple on-page sidebar sub-navigations by selecting pages and saving them to DB. I want them to then be able to save the menus, and include them as they see fit on any pages they want.

And here comes the problem. Initially I wanted to make it very simple - just create a SubMenu model storing all pageIds in json (to keep subpage structure):

Submenu Model
id | name[string] | page_ids[json]

However, I see quite a lot of problems with this approach. If we had, say, 5 menus of 10 pages on a page, after parsing the sumbenu model, I would need to individually get each page from the database (plus additionally some more, as i need to get parents' slugs too in order to build the URLs). This doesn't really seem very performant.

I assume a smarter way of doing it would be to get the whole collection of pages after user selects them, and somehow saving it in the database. And this I have no idea how to approach.

I looked at Laravel-menu plugin but I don't think it can achieve what I'm aiming for

Any ideas on how to tackle this would be much appreciated. There must be some best practices on how to do that, but I can't find any resources whatsoever.




via Klon

Advertisement