Skip to content

Instantly share code, notes, and snippets.

@Grummfy
Created February 25, 2020 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Grummfy/740a36752d03bbd6a8e9ac8e058e6276 to your computer and use it in GitHub Desktop.
Save Grummfy/740a36752d03bbd6a8e9ac8e058e6276 to your computer and use it in GitHub Desktop.
Breadcrumbs from spatie menu
<nav class="breadcrumb" aria-label="breadcrumbs">
<ul>
@foreach($elements as list($label, $url))
@if ($loop->last)
<li class="is-active">
<span aria-current="page">
{{ $label }}
</span>
</li>
@else
<li>
<a href="{{ $url ?? '#' }}">
{{ $label }}
</a>
</li>
@endif
@endforeach
</ul>
</nav>
<?php
namespace Monizze\Libs\Spatie\Menu\Laravel;
use Spatie\Menu\Item;
use Spatie\Menu\Menu as RootMenu;
use Spatie\Menu\Laravel\Menu as BaseMenu;
class Menu extends BaseMenu
{
// ...
public function breadcrumbs()
{
$elements = [];
$lookup = function(RootMenu $items) use (&$lookup, &$elements)
{
/* @var Item $item */
foreach ($items as $item)
{
// not an active part, skip
if (!$item->isActive())
{
continue;
}
// we found a menu, let's dig inside
if ($item instanceof RootMenu)
{
// first add the element to the breadcrumbs
$elements[] = [$item->prepend ?? $item->append ?? '', null];
// dig inside the items
$lookup($item);
continue;
}
// leaf => add it to the breadcrumbs
$elements[] = [$item->text(), $item->getUrl()];
}
};
$lookup($this);
return view('layouts.components.breadcrumbs', ['elements' => $elements]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment