Laravel dynamic breadcrumbs with links

人走茶凉 提交于 2019-11-29 07:56:48

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>

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.

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!

Mohamed Laabid

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!