tabuna / breadcrumbs

Laravel Breadcrumbs - An easy way to add breadcrumbs to your @Laravel app.
https://github.com/tabuna/breadcrumbs
MIT License
339 stars 20 forks source link

I can not send parameter to Breadcrumb #25

Open sametsahin opened 3 years ago

sametsahin commented 3 years ago

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 commented 3 years ago

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 (https://github.com/tabuna/breadcrumbs/issues/10#issuecomment-732791991)

Then the breadcrumbs will look like this:

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

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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 commented 3 years ago

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

tabuna commented 3 years ago

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 commented 3 years ago

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))
   );