Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel dynamic breadcrumbs with links

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.

like image 588
Wasif Iqbal Avatar asked Nov 28 '22 16:11

Wasif Iqbal


2 Answers

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>
like image 154
user1669496 Avatar answered Dec 04 '22 05:12

user1669496


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!

like image 27
Rohan Khude Avatar answered Dec 04 '22 04:12

Rohan Khude