madsem's avatar

Laravel 5.6 Bootstrap 4 Breadcrumb as Blade partial: Full code

I just made this because I needed it and did not want to install a package just for this. And because it works really nicely I thought I'd leave it here for my fellow Laracasters in case any of you needs something like this now or in the future.

It does not create a bread crumb segment for url-prefix and hides segments that contain only numbers (IDs).

@if(Route::current()->getName() != 'home')
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb mt-2">
            <li class="breadcrumb-item"><a href="{{ route('home') }}">Home</a></li>
            @php
                $link = url('/');
            @endphp
            
            @foreach(request()->segments() as $segment)
                @php
                    $link .= "/" . request()->segment($loop->iteration);
                @endphp
                @if(rtrim(request()->route()->getPrefix(), '/') != $segment && ! preg_match('/[0-9]/', $segment))
                    <li class="breadcrumb-item {{ $loop->last ? 'active' : '' }}">
                        @if($loop->last)
                            {{ title_case($segment) }}
                        @else
                            <a href="{{ $link }}">{{ title_case($segment) }}</a>
                        @endif
                    </li>
                @endif
            @endforeach
        </ol>
    </nav>
@endif 
0 likes
1 reply

Please or to participate in this conversation.