A
A
an5432019-03-13 05:22:49
Laravel
an543, 2019-03-13 05:22:49

How to make breadcrumbs in Laravel if there are many categories for a post?

Greetings! I'm using the davejamesmiller/laravel-breadcrumbs package . But I can have 1 post in many categories, so $post->categoryit doesn’t work, tell me how best to make crumbs or transfer crumbs from where I came to the post.

// Home > Blog > [Category]
Breadcrumbs::for('category', function ($trail, $category) {
    $trail->parent('blog');
    $trail->push($category->title, route('category', $category->id));
});

// Home > Blog > [Category] > [Post]
Breadcrumbs::for('post', function ($trail, $post) {
    $trail->parent('category', $post->category);
    $trail->push($post->title, route('post', $post->id));
});

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
Konstantin, 2019-03-13
@an543

A post in several categories and available at different urls is very bad in terms of SEO promotion (duplicate pages). Consider having a separate Url to view the post at /home/show/post . (somehow done in wordpress) And in breadcrumbs there will be only the main and post page, without a category.

K
Konstantin B., 2019-03-13
@Kostik_1993

I personally did this at one store.
A product can be added to multiple categories. The product is available at one address - without category prefixes /product/product_slug
And I received breadcrumbs only for the first category that the product has.
That is, in your case, instead of $post->category should be $post->categories->first()
Then what the guys above write is not at all from this question

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question