diglactic / laravel-breadcrumbs

Laravel Breadcrumbs - A simple Laravel-style way to create breadcrumbs.
https://packagist.org/packages/diglactic/laravel-breadcrumbs
MIT License
868 stars 63 forks source link

Multiple route binding #70

Closed lintangtimur closed 1 year ago

lintangtimur commented 1 year ago

How to make breadcrum from route like this

Route::get("category/{category:name}/video/{post:title_slug}", [DashboardController::class, "video_detail"])->name("video.title");

My expectation

Home > {Dynamic Category Name} > {Dynamic Video title}

My code

// Home
Breadcrumbs::for('dashboard', function (BreadcrumbTrail $trail) {
    $trail->push('Home', route('dashboard'));
});

// Home > Category:name
Breadcrumbs::for('video.category', function (BreadcrumbTrail $trail, \App\Models\Category $category) {
    $trail->parent('dashboard');
    $trail->push($category->name, route('video.category', $category->name));
});

// Home > Blog > [Video:title]
Breadcrumbs::for('video.title', function (BreadcrumbTrail $trail, \App\Models\Category $category, \App\Models\Post $post) {
    $trail->parent('video.category');
    $trail->push("asd",route("video.title",[ "category"=>$post->category->name, "post"=>$post->title_slug]));
});

Error response

Too few arguments to function Diglactic\Breadcrumbs\ServiceProvider::{closure}(), 1 passed in D:\Code\vendor\diglactic\laravel-breadcrumbs\src\Generator.php on line 64 and exactly 2 expected

shengslogar commented 1 year ago

You'll need to pass along relevant arguments when calling ->parent.

E.g. $trail->parent('video.category', $category);.

lintangtimur commented 1 year ago

Thank you

ahmadyousefdev commented 9 months ago

for anyone else, this is another way:

Breadcrumbs::for('aspect.show', function (BreadcrumbTrail $trail, Category $category, Article $article) {
    $trail->parent('category.show', $category);
    $trail->push($article->title, route('article.show', ['article' => $article, 'category' => $category]));
});

and in blade:

{{ Breadcrumbs::render('article.show', $category, $article) }}

i thinks this should be added to the readme , it'll be useful

shengslogar commented 9 months ago

@ahmadyousefdev Not sure how this relates to the original issue. The behavior of passing multiple params to Breadcrumbs::render is suggested here, but could be more explicit. PRs to improve documentation are always welcome.