A new Laravel Navigation package has been released by Spatie. You can check the link to get access to this package for installation and its usage. Although, we can still get some idea of that package here in this tutorial too.
Laravel Navigation provides a base to create navigational elements like menus and breadcrumbs. Laravel Navigation doesn’t do any HTML generation but it can give you the flexibility to build your own UI for navigation trees and active state. You can use this package as a “renderless component” for navigation components:
app(Navigation::class) ->add('Home', route('home')) ->add('Blog', route('blog.index'), function (Section $section) { $section ->add('All posts', route('blog.index')) ->add('Topics', route('blog.topics.index')); }) ->addIf(Auth::user()->isAdmin(), function (Navigation $navigation) { $navigation->add('Admin', route('admin.index')); });
A navigation object can be rendered to a tree, or to breadcrumbs. i.e. /blog/topics/laravel
// Render to tree app(Navigation::class)->tree();
[ { "title": "Home", "url": "/", "active": false, "children": [] }, { "title": "Blog", "url": "/blog", "active": false, "children": [ { "title": "All posts", "url": "/blog", "active": false, "children": [] }, { "title": "Topics", "url": "/blog/topics", "active": true, "children": [] } ], }, { "title": "Admin", "url": "/admin", "active": false, "children": [] } ]
This package will also help you generate breadcrumbs from navigation using the following method:
// Append additional pages in your controller app(Navigation::class)->activeSection()->add($topic->name, route('blog.topics.show', $topic)); // Render to breadcrumbs app(Navigation::class)->breadcrumbs();
[ { "title": "Blog", "url": "/blog" }, { "title": "Topics", "url": "/blog/topics" }, { "title": "Laravel", "url": "/blog/topics/laravel" } ]
You can install the package via composer:
composer require spatie/laravel-navigation
The source of this article is
You can learn about the spatie laravel navigation package, installation instructions, and a detailed description on GitHub.
To get know more about the Laravel, you can check these articles too.