Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Breadcrumbs Laravel Package

tabuna/breadcrumbs

Laravel breadcrumbs made easy: define breadcrumb trails right in your route definitions with a fluent API (parent/push), automatic route detection, and support for request parameters and model binding to generate consistent navigation across your app.

View on GitHub
Deep Wiki
Context7

Introduction

Breadcrumbs display a list of links indicating the position of the current page in the whole site hierarchy. For example, breadcrumbs like Home / Sample Post / Edit means the user is viewing an edit page for the "Sample Post." He can click on "Sample Post" to view that page or click on "Home" to return to the homepage.

Home / Sample Post / Edit

This package for the Laravel framework will make it easy to build breadcrumbs in your application.

Installation

Run this at the command line:

composer require tabuna/breadcrumbs

This will update composer.json and install the package into the vendor/ directory.

Define your breadcrumbs

Now you can define breadcrumbs directly in the route files:

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'))
);

You may also retrieve arguments from the request. Parameters are resolved by name:

Route::get('/category/{category}', function (Category $category) {
    // In this example, $category is an Eloquent model instance.
    // ...
})
    ->name('category')
    ->breadcrumbs(fn (Trail $trail, Category $category) =>
        $trail->push($category->title, route('category', $category->id))
);

Route detection

The package tries to reduce the number of lines needed. For this, you can skip passing the results of the route() methods. The following two declarations will be equivalent:

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

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

Like to use a separate route file?

You can do this simply by adding the desired file to the service provider

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class BreadcrumbsServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap the application events.
     */
    public function boot(): void
    {
        require base_path('routes/breadcrumbs.php');
    }
}

Then it will be your special file in the route directory:

// routes/breadcrumbs.php


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

Route resource

When using resources, a whole group of routes is declared for which you must specify values manually

// routes/web.php

Route::resource('photos', 'PhotoController');

It’s better to specify this in service providers, since route files can be cached

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Tabuna\Breadcrumbs\Breadcrumbs;
use Tabuna\Breadcrumbs\Trail;

class BreadcrumbsServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     */
    public function boot(): void
    {
        Breadcrumbs::for('photos.index', fn (Trail $trail) =>
             $trail->push('Photos', route('home'))
        );
        
        Breadcrumbs::for('photos.create', fn (Trail $trail) =>
            $trail
                ->parent('photos.index', route('photos.index'))
                ->push('Add new photo', route('home'))
        );
    }
}

Output the breadcrumbs use Blade Component

You can use the output component:

<ul>
    <x-tabuna-breadcrumbs/>
</ul>

To define classes of list items, you can specify:

<x-tabuna-breadcrumbs
  class="item"
  active="active"
/>

You can also pass parameters:

<x-tabuna-breadcrumbs
    :parameters="[
        'foo' => 'Dashboard',
        'baz' => 'Users',
        'buzz' => 'Edit Profile'
    ]"
/>

And call named routes explicitly:

<x-tabuna-breadcrumbs
    route="static"
/>

Output the breadcrumbs use Blade view

In order to display breadcrumbs on the desired page, simply call:

@if(Breadcrumbs::has())
    @foreach (Breadcrumbs::current() as $crumbs)
        @if ($crumbs->url() && !$loop->last)
            <li class="breadcrumb-item">
                <a href="{{ $crumbs->url() }}">
                    {{ $crumbs->title() }}
                </a>
            </li>
        @else
            <li class="breadcrumb-item active">
                {{ $crumbs->title() }}
            </li>
        @endif
    @endforeach
@endif

And results in this output:

Home / About

Credits

For several years, I successfully used the Dave James Miller package to solve my problems, but he stopped developing and supporting it. After a long search for alternatives, I liked the Dwight Watson package, but the isolation of breadcrumbs from the announcement of the routes did not give me rest. That's why I created this package. It uses the code of both previous packages.

License

The MIT License (MIT). Please see License File for more information.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky