Let's get things started by building a simple menu with two links. All of the following examples are using classes from the Spatie\Menu
namespace.
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>
Menu's are created via the new
factory method.
$menu = Menu::new();
Links are created with the to
factory method, which requires a url and a string of text (or html) as parameters.
$link = Link::to('/', 'Home');
A menu can be instantiated with an array of items, or items can be added fluently with the add
method.
$menu = Menu::new([
Link::to('/', 'Home'),
Link::to('/about', 'About'),
]);
$menu = Menu::new()
->add(Link::to('/', 'Home'))
->add(Link::to('/about', 'About'));
When we render or echo the menu, it will output our intended html string.
echo $menu->render();
echo $menu;
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about">About</a></li>
</ul>