Skip to content
This repository has been archived by the owner on Jan 23, 2024. It is now read-only.

Latest commit

 

History

History
108 lines (77 loc) · 2.73 KB

outputting-breadcrumbs.md

File metadata and controls

108 lines (77 loc) · 2.73 KB
title sort
Outputting Breadcrumbs
4

Introduction

Call Breadcrumbs::render() in the view for each page, passing it the name of the breadcrumb to use an any additional parameters.

With Blade

In the page (e.g. resources/views/home.blade.php):

{!! Breadcrumbs::render('home') !!}

Or with a parameter:

{!! Breadcrumbs::render('category', $category) !!}

With Blade Layouts and @section

In the page (e.g. resources/views/home.blade.php):

@extends('layouts.master')

@section('breadcrumbs')
    {!! Breadcrumbs::render('home') !!}
@endsection

Or using the shorthand syntax:

@extends('layouts.master')

@section('breadcrumbs', Breadcrumbs::render('home'))

And in the layout file (e.g. resources/views/layouts/master.blade.php):

@yield('breadcrumbs')

Pure PHP (without Blade)

In the page (e.g. resources/views/home.php):

<?php echo Breadcrumbs::render('home'); ?>

Structured Data

To render breadcrumbs as JSON-LD structured data (usually for SEO reasons), use Breadcrumbs::view() to render the breadcrumbs::json-ld template in addition to the normal one. For example:

<html>
    <head>
        ...
        {!! Breadcrumbs::view('breadcrumbs::json-ld', 'category', $category) !!}
        ...
    </head>
    <body>
        ...
        {!! Breadcrumbs::render('category', $category) !!}
        ...
    </body>
</html>

{note} If you use Laravel Page Speed you may need to disable the TrimUrls middleware.

To specify an image, add it to the $data parameter in push():

Breadcrumbs::for('post', function (Generator $trail, $post) {
    $trail->parent('home')
        ->push($post->title, route('post', $post), ['image' => asset($post->image)]);
});

If you prefer to use Microdata or RDFa, you will need to create a custom template.

Blade Component

As of version 1.1.0, you can use the provided <x-breadcrumbs /> blade component to render your breadcrumbs. By default, it uses the current route name to determine if there are breadcrumbs that exist to render. You can manually specify which breadcrumbs you want rendered as well:

<x-breadcrumbs breadcrumbs="your.breadcrumbs.route_name" />

<!-- with parameters -->
<x-breadcrumbs breadcrumbs="your.breadcrumbs.route_name" :params="[$user]" />

You can also pass in false as the breadcrumbs attribute if you need to disable the rendering of the breadcrumbs altogether:

<x-breadcrumbs :breadcrumbs="false" />