Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Vishnu's avatar

Breadcrumbs

Is there an easy way of displaying breadcrumbs in Laravel? Would be great if there was a quick short lesson for that one. Or if I have missed it can someone please kindly point it to me. Thanks a lot!

0 likes
17 replies
Vishnu's avatar

Thanks a lot @ovvessem and @HRcc :) I am looking for something simple. But shall check out both of them and see which one works best for me. Thanks again!

freekmurze's avatar

+1 for the davejamesmiller/laravel-breadcrumbs package. Use it in all my projects.

1 like
MattMangoni's avatar

+1 for davejamesmiller/laravel-breadcrumbs. great stuff :)

superassist's avatar

I just use simple Facade for that like below:

BC::route('books.index', 'Books')
  ->route('books.show', $book->title, $book->id)
  ->to("/books/{$book->id}/edit", 'Edit');

Do you have any other ways? Guys!

mezie's avatar

+1 for davejamesmiller/laravel-breadcrumbs. great documentation, easy for usage and highly recommended.

1 like
MikeHopley's avatar

Another option:

If your content is inherently hierarchical in the database, then breadcrumbs are easy. For example, I use the Baum package to apply the nested set pattern to my content.

Each of my content pages has a "crumb" column on the DB. This is the breadcrumb text, and the url fragment (or "folder") is generated from this by lowercasing it and replacing spaces with hyphens -- e.g. "Some category" --> "some-category'.

Building breadcrumbs then becomes very easy:


class Breadcrumb {

    public static function render(ContentPage $page)
    {
        $crumbs = self::getCrumbs($page);
        return self::renderCrumbs($crumbs);
    }

    public static function getCrumbs($page)
    {
        $crumbs = [];

        foreach ( $page->getAncestors() as $key => $node )
        {
            $parent = '/';
            if ( $key > 0)
            {
                $parent = $crumbs[$key-1]['url'] . '/';
            }

            $crumbs[] = [
                'title' => $node->crumb,
                'url'   => $parent . self::crumbToUrl($node->crumb),
            ];
        }

        // Current page
        $crumbs[] = [
            'title' => $page->title,
            'url'   => end($crumbs)['url'] . '/' . self::crumbToUrl($page->crumb),
        ];

        return $crumbs;
    }

    private static function crumbToUrl($crumb)
    {
        $title = str_replace( ' ', '-', $crumb);
        return Str::lower($title);
    }

    private static function renderCrumbs($crumbs)
    {
        $currentPage = array_pop($crumbs);

        $html = '<ul class="breadcrumbs">';
        foreach ($crumbs as $crumb)
        {
            $html .= '<li><a href="'.$crumb['url'].'">'.$crumb['title'].'</a></li>';
        }
        $html .= '<li class="active">'.$currentPage['title'].'</li></ul>';

        return $html;
    }

Note that the getAncestors() method comes from Baum.

InspiredPrynce's avatar

+1 for the davejamesmiller/laravel-breadcrumbs package. Use it in all my projects.

usamaxyz's avatar

+1 for the davejamesmiller/laravel-breadcrumbs package. It's really great.

klimov-paul's avatar

You can implement breadcrumbs yourself in easy way without usage of extra libraries.

Create some view file - 'resources/views/includes/breadcrumbs.blade.php', which should contain the breadcrumbs HTML rendering logic. Here you can define your project-specific markup, place link to the Home page (since it usually always present) and so on. For example:

@if (! empty($breadcrumbs))
<ul class="breadcrumbs">
  <li class="breadcrumbs-bullet">
    <a href="{{ route('home') }}" class="breadcrumbs-link">
      <span class="breadcrumbs-link-text">Home</span>
    </a>
  </li>
  @foreach ($breadcrumbs as $label => $link)
  <li class="breadcrumbs-bullet">
    @if (is_int($label) && ! is_int($link))
    <a class="breadcrumbs-link">
      <span>{{ $link }}</span>
    </a>
    @else
    <a href="{{ $link }}" class="breadcrumbs-link">
      <span class="breadcrumbs-link-text">{{ $label }}</span>
    </a>
    @endif
  </li>
  @endforeach
</ul>
@endif

Then you can render breadcrumbs anytime using regular @include directive:

<section class="header">
@include('includes.breadcrumbs', ['breadcrumbs' => [
    'Foo' => route('foo'),
    'Bar' => route('bar'),
    'This page',
]])
</section>
...

You can wrap include into @section directive allowing passage of the content to the layout:

{{-- view 'pages.about' --}}
@section('breadcrumbs')
@include('includes.breadcrumbs', ['breadcrumbs' => [
    'About Us',
]])
@endsection
...

{{-- view 'layouts.main --}}
<section class="header">
@yield('breadcrumbs')
</section>
...

In order to create easier syntax you can register your own Blade directive @breadcrumbs in following way:

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\View\Compilers\BladeCompiler;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        $this->app->extend('blade.compiler', function (BladeCompiler $bladeCompiler) {
            $bladeCompiler->directive('breadcrumbs', function ($expression) {
                return "<?php $__env->startSection('breadcrumbs'); ?>\n"
                    ."<?php echo $__env->make('includes.breadcrumbs', \Illuminate\Support\Arr::except(array_merge(get_defined_vars(), ['breadcrumbs' => {$expression}]), ['__data', '__path']))->render(); ?>\n"
                    ."<?php $__env->stopSection(); ?>\n";
            });

            return $bladeCompiler;
        });
    }
}

Doing so you will be able to render your breadcrumbs view into a section with the single directive:

{{-- view 'items.show' --}}
@breadcrumbs([
    'Items' => route('items.index'),
    $item->name,
])
...

{{-- view 'layouts.main --}}
<section class="header">
@yield('breadcrumbs')
</section>
...
4 likes
Troj's avatar

@klimov-paul This works great if you dont have 2 or more wildcards after eachother in your route. My routes look like this

Route::get('{continent}/{country}', 'ContriesController@show')->name('country.show');

And i add this to my country.blade.view:

@section('breadcrumbs')

@include('partials.breadcrumbs', ['breadcrumbs' => [
$country->name => route('country.index'),
$city->name,
]])

@endsection

I get this message: Missing required parameters for [Route: country.show] [URI: {continent}/{country}]

Any idea how to fix that?

Troj's avatar

Figured it out, i had to include the wildcards as parameters like this:

My route is like this:

Route::get('{continent}/{country}', 'CountriesController@index')->name('country.index');

And that becomes this:

$country->name => route("country.index", ['continent' => $continent->slug,'country' => $country->slug]),

1 like

Please or to participate in this conversation.