My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Laravel Breadcrumbs

Alexandr Chernyaev's photo
Alexandr Chernyaev
·Jun 3, 2020

For the past two years, I have used the only breadcrumbs package from Dave James Miller and it was a wonderful experience. Unfortunately, its author decided to stop developing and supporting this package. It is said that such a popular package was not transferred to anyone.

But our projects still need to create bread crumbs, so I used the opening time to create my own package with some improvements that I think.

In the past, I was a little disappointed with the fact that spelling crumbs were torn off routes. Therefore, for a large application, you first had to open the web.php file, find the route I needed, copy its name, go to another file and find it already there.

To fix this, we can indicate breadcrumbs right when declaring a route:

use Tabuna\Breadcrumbs\Trail;

// Home
Route::get('/', fn () => view('home'))
    ->name('home')
    ->breadcrumbs(fn (Trail $trail) =>
        $trail->push('Home', route('home'))
);

// Home > About
Route::get('/about', fn () => view('home'))
    ->name('about')
    ->breadcrumbs(fn (Trail $trail) =>
        $trail->parent('home')->push('About', route('about'))
);

In addition, to maintain compatibility, we can declare them in a separate file:

// routes/breadcrumbs.php

// Photos
Breadcrumbs::for('photo.index', fn (Trail $trail) =>
    $trail->parent('home')->push('Photos', route('photo.index'))
);