Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can not send parameter to Breadcrumb #25

Open
sametsahin opened this issue Jan 6, 2021 · 11 comments
Open

I can not send parameter to Breadcrumb #25

sametsahin opened this issue Jan 6, 2021 · 11 comments

Comments

@sametsahin
Copy link

sametsahin commented Jan 6, 2021

Dear All Friend,

Firstly I want to say thank you for your help.

I am using breadcrumbs in my project. There is no problem in pages where I dont send parameter. But when i want to send parameter, I dont taking anything. You can see below detail code in my project

BreadCrumbsServiceProvider

require base_path('routes/breadcrumbs.php'); // (I regestered this file in config/app)

breadcrumbs.php

// Setting ->There is no problem
Breadcrumbs::for('admin.setting.index', fn(Trail $trail) => $trail
    ->parent('admin.dashboard')
    ->push('Site Ayarları', route('admin.setting.index'))
);
// Client Detail->There is huge problem for me :)
Breadcrumbs::for('admin.client.detail/{id}', fn(Trail $trail, Client $client) => $trail
    ->parent('admin.musteri.index', route('admin.musteri.index'))
    ->push($client->id . ' Nolu Müşteri Detayı', route('admin.client.detail', $client->id))
);

You think, what should I do?

@tabuna
Copy link
Owner

tabuna commented Jan 6, 2021

Hey @sametsahin. I am assuming you have your route declared as follows:

Route::get('/admin/client/detail/{client}', function (Client $client) {
    //code...
})->name('admin.client.detail');

Pay attention to the names that we expect (#10 (comment))

Then the breadcrumbs will look like this:

Breadcrumbs::for('photos.index', fn (Trail $trail, Client $client) =>
     $trail->push('...')
);

@sametsahin
Copy link
Author

sametsahin commented Jan 7, 2021

Hello My Friend,

I send client->id parameter and I call this parameter in breadcrumbs but no change. If we find solition, you will save me :)
The final code is below.
web.php

    Route::get('edit/client/{id}', [ClientController::class, 'edit'])->name('edit.client');
breadcrumbs.php
  Breadcrumbs::for('admin.edit.client/{id}', fn(Trail $trail, Client $client) => $trail
      ->parent('admin.home.client')
      ->push($client->id . ' Edit Client', route('admin.edit.client', $client->id))
  );

Link->http://127.0.0.1:8000/admin/edit/client/7 //There is not any problem about working but no show breadcrumbs

Thank You

@tabuna
Copy link
Owner

tabuna commented Jan 7, 2021

No problem buddy. Just use the same names. Like this:

Route:

Route::get('edit/client/{client}', [ClientController::class, 'edit'])->name('edit.client');

Breadcrumbs:

Breadcrumbs::for('edit.client', fn(Trail $trail, Client $client) => $trail
    ->parent('admin.home.client')
    ->push($client->id . ' Edit Client', route('admin.edit.client', $client->id))
);

Controller:

public function edit(Client $client) {
    // ...
}

@sametsahin
Copy link
Author

Hello Buddy,

Unfortunately fail again.

web
Route::get('duzenle/musteri/{client_id}', [ClientController::class, 'edit'])->name('edit.client');

Breadcrumbs

Breadcrumbs::for('admin.edit.client', fn(Trail $trail, Client $client_id) => $trail
    ->parent('admin.home.client')
    ->push(' Edit Client', route('admin.edit.client', $client_id->id))
);

Controller
public function edit($client_id){...

It seems error
App\Providers\BreadcrumbsServiceProvider::{closure}(): Argument #2 ($client_id) must be of type App\Models\Client, string given (View:

When i deleted "Client" in Breadcrumbs ->, fn(Trail $trail, $client_id) => $trail
Attempt to read property "id" on string (View:

@tabuna
Copy link
Owner

tabuna commented Jan 7, 2021

Change:

public function edit($client_id){...

on:

public function edit(Client $client_id){...

You are either working with an object. Or with a value. You cannot expect an object in one place and an integer/string in another.

@sametsahin
Copy link
Author

I tried buddy, When i change line url is broken. 404 not found
public function edit(Client $client_id){...

@tabuna
Copy link
Owner

tabuna commented Jan 7, 2021

Suppose the controller cannot find your model. Why should bread crumbs do this? It will help if you read https://laravel.com/docs/8.x/routing#parameters-and-dependency-injection as well as https://laravel.com/docs/8.x/routing#route-model-binding

From the documentation:

Since the $user variable is type-hinted as the App\Models\User Eloquent model and the variable name matches the {user} URI segment, Laravel will automatically inject the model instance that has an ID matching the corresponding value from the request URI. If a matching model instance is not found in the database, a 404 HTTP response will automatically be generated.

@nviz
Copy link

nviz commented Jun 16, 2021

Just do it like this;

   Breadcrumbs::for('admin.client.detail/{id}', fn(Trail $trail, $id) => $trail
       ->parent('admin.musteri.index', route('admin.musteri.index'))
       ->push(Client::find($id)->first()->id . ' Nolu Müşteri Detayı', route('admin.client.detail', $id))
   );

@macit-emre
Copy link

i am failing in this issue too.

Route::screen('integrated-accounts/{integratedAccount}', \App\Orchid\Screens\IntegratedAccountsScreen::class)->name('platform.integrated-accounts')->breadcrumbs(function (Trail $trail, \App\Models\IntegratedAccount $integratedAccount) {
        return $trail
            ->parent('platform.index')
            ->push(__('Integrated Accounts'));
    });

resulting :
Illuminate\Routing\RouteFileRegistrar::{closure}(): Argument #2 ($integratedAccount) must be of type App\Models\IntegratedAccount, string given

i am on orchid14 and laravel 11

@macit-emre
Copy link

i think this issue happens when you use 2 variables in a route and use same variable in a different route. (in the requested breadcrumb chain, 2 or greater chain needed)

@macit-emre
Copy link

these 2 routes makes the error happen. They are parent - child in breadcrumb tree.
the first route resolves $integratedAccount as EloquentModel
the second route resolves $integratedAccount as string.

 Route::screen('integrated-accounts/{integratedAccount}/orders', \App\Orchid\Screens\IntegratedAccountOrdersScreen::class)->name('platform.integrated-account-orders')->breadcrumbs(function (Trail $trail, $integratedAccount) {
        return $trail
            ->parent('platform.integrated-accounts',\route('platform.integrated-accounts'))
            ->push(__('Integrated Account Orders'));
    });
    Route::screen('integrated-accounts/{integratedAccount}/orders/{orderId}', \App\Orchid\Screens\IntegratedAccountOrderScreen::class)->name('platform.integrated-account-single-order')->breadcrumbs(function (Trail $trail, $integratedAccount, $orderId) {
        return $trail
            ->parent('platform.integrated-account-orders', \route('platform.integrated-account-orders', ['integratedAccount' => $integratedAccount]))
            ->push(__('Integrated Account Order Detail'));
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants