5

I am trying to implement dynamic breadcrumbs in laravel with links. I successfully render the breadcrumbs but without links by following code.

    <ol class="breadcrumb">
    <li><a href="#"><i class="fa fa-dashboard"></i>Marketplace</a></li>
    @foreach(Request::segments() as $segment)
    <li>
        <a href="#">{{$segment}}</a>
    </li>
    @endforeach
</ol>

But now i am facing issue with the urls. I am getting the current url of the route with all the decendents. Can someone please help me that how can I add links to the breadcrumbs ?

Thanks.

Wasif Iqbal
  • 474
  • 1
  • 4
  • 17
  • Provide url sample URL and generated Breadcrumb, so we can help you accordingly – Qazi Jun 22 '16 at 11:31
  • 1
    I recommend https://github.com/davejamesmiller/laravel-breadcrumbs – Jeff Jun 22 '16 at 12:26
  • @Qazi Url will be something like this: http://marketplace.dev/admin/types/create And the breadcrumbs should be like admin > types > create – Wasif Iqbal Jun 22 '16 at 18:01
  • @Jeff i checked that, that would be my second options. I was trying to handle it with a simple function. Because above code giving me routes array as I needed. I just want to know that if i got some function where i could send each route name like in the following URL http://marketplace.dev/admin/types/create if i send "types" then it gives the current controller or path. so that I can this with only one function. Please let me know if I am going on the right way or should I use breadcrumbs library right away ? Thanks. – Wasif Iqbal Jun 22 '16 at 18:05
  • @Jeff, i used that library and its awesome. Working great on laravel 5. Changed my mind of handling breadcrumbs by my own function. – Wasif Iqbal Jun 28 '16 at 11:39

6 Answers6

13

If I understand your issue correctly, you just need to fill in the URL of the link. This is untested but I think it should work.

<ol class="breadcrumb">
    <li><a href="#"><i class="fa fa-dashboard"></i>Marketplace</a></li>
    <?php $segments = ''; ?>
    @foreach(Request::segments() as $segment)
        <?php $segments .= '/'.$segment; ?>
        <li>
            <a href="{{ $segments }}">{{$segment}}</a>
        </li>
    @endforeach
</ol>
user1669496
  • 32,176
  • 9
  • 73
  • 65
  • 1
    Thanks for your response. I already tried that but it create issues with other links and with namespaces as well. I think i should go with some library instead of this. Anyway thanks for your help. :) – Wasif Iqbal Jun 26 '16 at 19:18
12

This worked for me, tried in Laravel 5.4.*

Requirement for this code to work flawlessly: All URL's should have hierarchy pattern in your routes file

Below code will create crumb for each path -

<a href="/">Home</a> >                
<?php $link = "" ?>
@for($i = 1; $i <= count(Request::segments()); $i++)
    @if($i < count(Request::segments()) & $i > 0)
    <?php $link .= "/" . Request::segment($i); ?>
    <a href="<?= $link ?>">{{ ucwords(str_replace('-',' ',Request::segment($i)))}}</a> >
    @else {{ucwords(str_replace('-',' ',Request::segment($i)))}}
    @endif
@endfor

So Breadcrumb for URL your_site.com/abc/lmn/xyz will be - Home > abc > lmn > xyz

Hope this helps!

Rohan Khude
  • 4,455
  • 5
  • 49
  • 47
  • 2
    Thanks this works well. For this small and simple code, why would I use package. Thanks again. I don't know why no one giving up vote. – James Norman May 01 '18 at 17:24
  • 1
    This code is working fantastic for me so far. I created a partial template for it so I don't have to duplicate the code. I pasted the code into `~/views/breadcrumbs.blade.php` and then call it as `@include('breadcrumbs')`. – agm1984 Feb 05 '19 at 17:54
  • 1
    I don't understand why people recommending packages when this works perfectly!! – Yuvraj Mudaliar Sep 07 '21 at 03:36
5

Im not sure if you already got a solution for this, but i figured out a way to go about it on my project. It might come in handy for your implementation.

I ended up with either adding the whole url to the link or only the segment, which ofc is not desirable, so using array slice i start slicing from the 0 index in the array and only slice untill the current iteration of the loop, then implode the array into a string and then use URL::to to create the link.

<ol class="breadcrumb">
   <li>
       <i class="fa fa-home"></i>
       <a href="{{route('admin.index')}}">HOME</a>
   </li>

   @for($i = 2; $i <= count(Request::segments()); $i++)
      <li>
         <a href="{{ URL::to( implode( '/', array_slice(Request::segments(), 0 ,$i, true)))}}">
            {{strtoupper(Request::segment($i))}}
         </a>
      </li>
   @endfor
</ol>

As you will notice, I only start my iteration from 2 ($i = 2) as my app base url starts at /admin and i manually put my home Url in the first breadcrumb.

Again you might already have a solution, but throught this could work for people who do not want to add the package to get breadcrumbs.

Eamon
  • 96
  • 5
1

I wrote a this code that can handle laravel resource ( index | edit | create ) routes dynamically:

Custom Breadcrumb => custom.blade.php

@php
    $segments=[];
    $l=count(Request::segments())-1
@endphp

@switch(Request::segments()[$l])
    @case('edit')
        @php
            $l--;
            $segments=array_slice(Request::segments(),0,$l);
            $segments[]=$model->slug // Model that passed to this included blade file
        @endphp
    @break
    @default
        @php $segments=Request::segments() @endphp
@endswitch

@php
    $link=''
@endphp
@foreach($segments as $sg)
   @php $link.='/'.$sg @endphp
   @if($loop->index<$l)
      <li class="breadcrumb-item">
         <a href="{{$link}}">{{ucfirst($sg=='admin'?'home':$sg)}}</a>
      </li>
   @else
      <li class="breadcrumb-item active">
         {{ucfirst($sg)}}
      </li>
   @endif
@endforeach

Use Custom Breadcrumb => example.balde.php

@include('admin.vendor.breadcrumb.custom',['model'=> $articles])
Arash Younesi
  • 1,671
  • 1
  • 14
  • 23
0

Just add slash / before any link to add decendents to domain name like that

<a href="/YourLink" ></a>

Anbuselvan Rocky
  • 606
  • 6
  • 22
0

I liked the solution offered by @rohankhude but it was not showing "/" after the first segment (marketplace). So here is a clean code.

<div class="breadcrumb">
    <ul>
        <li>
            <a href="{{ route('welcome') }}" class="inactive">Home</a>
        </li>
                
        <?php $link = "" ?>
        @for($i = 1; $i <= count(Request::segments()); $i++)
            @if($i < count(Request::segments()) & $i > 0)
                <?php $link .= "/" . Request::segment($i); ?>
                @if ($i == 1)
                    <a href="<?= $link ?>" class="inactive">/ {{ ucwords(str_replace('-',' ',Request::segment($i)))}}</a>
                @else
                    <a href="<?= $link ?>" class="inactive">{{ ucwords(str_replace('-',' ',Request::segment($i)))}}</a>
                @endif
            @else
                <a class="active">/ {{ucwords(str_replace('-',' ',Request::segment($i)))}}</a>
            @endif
        @endfor
    </ul>
</div>
Akash Sethi
  • 184
  • 1
  • 4
  • 15