Have a look at - https://github.com/davejamesmiller/laravel-breadcrumbs
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!
Use the above mentioned package if you need something highly configurable or you can use CreITive/Laravel-4-Breadcrumbs if you want someting simple and easier to use.
+1 for the davejamesmiller/laravel-breadcrumbs package. Use it in all my projects.
@freekmurze yes it does look intuitive. Shall give it a try :)
+1 for davejamesmiller/laravel-breadcrumbs. great stuff :)
WOW highly recommend https://github.com/davejamesmiller/laravel-breadcrumbs
got it working quickly
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!
+1 for davejamesmiller/laravel-breadcrumbs. great documentation, easy for usage and highly recommended.
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.
+1 for the davejamesmiller/laravel-breadcrumbs package. Use it in all my projects.
davejamesmiller/laravel-breadcrumbs is no longer maintained. But @maddhatter has created a fork for Laravel 5.4 here:maddhatter/laravel-breadcrumbs.
I love this plugin so I hop he keeps updating it.
davejamesmiller/laravel-breadcrumbs is now working with Laravel 5.4
+1 for the davejamesmiller/laravel-breadcrumbs package. It's really great.
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>
...
@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,
]])
I get this message: Missing required parameters for [Route: country.show] [URI: {continent}/{country}]
Any idea how to fix that?
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]),
Please or to participate in this conversation.