tabuna/breadcrumbs

Argument 2 passed to App\Providers\BreadcrumbsServiceProvider

fbc opened this issue · 6 comments

fbc commented

I'm getting this error:

Argument 2 passed to App\Providers\BreadcrumbsServiceProvider::App\Providers{closure}() must be an instance of App\Models\project\Project, string given (View: /var/www/resources/views/navigation/breadcrumbs.blade.php)

In app/Providers/BreadcrumbsServiceProvider.php

<?php

namespace App\Providers;

use Tabuna\Breadcrumbs\Trail;
use App\Models\project\Project;
use Tabuna\Breadcrumbs\Breadcrumbs;
use Illuminate\Support\ServiceProvider;

class BreadcrumbsServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Breadcrumbs::for('home', fn (Trail $trail) =>
        $trail->push('Dashboard', route('home')));

        Breadcrumbs::for('project.index', fn (Trail $trail) =>
        $trail->parent('home', route('home'))
            ->push('Projects', route('project.index')));

        Breadcrumbs::for('project.show', fn (Trail $trail, Project $project) =>
        $trail->parent('project.index', route('project.index'))
            ->push('View Project', route('project.show', compact('project'))));

        Breadcrumbs::for('project.bill.index', fn (Trail $trail, Project $project) =>
        $trail->parent('project.show', route('project.show', compact('project')))
            ->push('View Bills', route('project.bill.index', compact('project'))));
    }
}

in resources/views/navigation/breadcrumbs.blade.php

<div class="col-md-12">
    <div class="row">
        @if(Breadcrumbs::has())
        @foreach (Breadcrumbs::current() as $crumbs)
        @if ($crumbs->url() && !$loop->last)
        <li class="breadcrumb-item" style="list-style-type: none;">
            <a href="{{ $crumbs->url() }}">
                {{ $crumbs->title() }}
            </a>
        </li>
        @else
        <li class="breadcrumb-item active" style="list-style-type: none;">
            {{ $crumbs->title() }}
        </li>
        @endif
        @endforeach
        @endif
    </div>
</div>

I can't seem to figure out what I'm doing wrong. It works fine until I visit project.bill.index

My routes are defined as:

...
Route::resource('project', 'project\ProjectController'); // Project Crud
...
...
Route::resource('project.bill', 'project\bill\BillController'); // Bill Crud
...

Hey, @fbc, I have not been able to reproduce the problem. My route opens perfectly: project.bill.index.
Is there any other data that could influence? I used laravel 8 and package with the latest version

fbc commented

@tabuna I'm using Laravel 7 and ver 1.3.1 of your package.

@fbc I'm not sure if you figured this out yet, but see #10 (comment). In your case, the BillController::index method should look like:

public function index(Project $project) { ... }

to avoid the error you are getting.

I'm also running the same version as you.

@fbc
Did you try in parent function put the name and the object without route as the second argument

Breadcrumbs::for('booths.banners.create', fn (Trail $trail, Booth $booth) =>
$trail
    ->parent('booths.banners.index', $booth)
    ->push('Create Booth Banner', route('booths.banners.create', $booth->id)));

help me pls( im newbee

my controller

image

when i not use breadcums all worked
Route::get('/plastinky/all/{id}', [\App\Http\Controllers\PlastinkyController::class, 'ShowOne'])
->name('plastinky-one');
image

when i use
Route::get('/plastinky/all/{id}', [\App\Http\Controllers\PlastinkyController::class, 'ShowOne'])
->name('plastinky-one')
->breadcrumbs(fn (Trail $trail) => $trail->parent('plastinky')->parent('plastinky-data')->push('VIEW', route('plastinky-one'))
);

then i have this error, how i can fix it?
image

Hey @misha1. Your mistake is not the same as that of the topic author.
Your route has a required id parameter that you forgot to pass to the breadcrumb.

use App\Http\Controllers\PlastinkyController;

Route::get('/plastinky/all/{id}', [PlastinkyController::class, 'ShowOne'])
  ->name('plastinky-one')
  ->breadcrumbs(
    fn(Trail $trail, $id) => $trail->parent('plastinky')
      ->parent('plastinky-data')
      ->push('VIEW', route('plastinky-one', $id))
  );