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

Cannot use Breadcrumbs::current() after switching from route-model binding to repository design pattern #15

Closed awsqed closed 3 years ago

awsqed commented 3 years ago

It was working just find when I was using route-model binding and Breadcrumbs::current()->title returns the correct title. But after switching to repository design pattern, now the $model object is just the normal ID of the model (I tried to dump the $model object).

This is the error I'm getting: Screenshot from 2021-01-25 15-58-02

breadcrumbs.php: https://controlc.com/bfa32022

Controller:

  public function edit($publisherId)
  {
      $this->authorize('update-publisher');

      $publisher = $this->repository->find($publisherId);

      return view('dashboard.publishers.edit', compact('publisher'));
  }

View: https://controlc.com/e57d6f00

dd($model) with Breadcrumbs::current()->title: Screenshot from 2021-01-25 16-04-45

dd($model) without Breadcrumbs::current()->title: Screenshot from 2021-01-25 16-06-17

shengslogar commented 3 years ago

This isn't a package issue. You'll need to find some way to load your model from the repository inside your breadcrumbs file, likely copying logic you already have inside your controller.

awsqed commented 3 years ago

This isn't a package issue. You'll need to find some way to load your model from the repository inside your breadcrumbs file, likely copying logic you already have inside your controller.

Sorry but I'm quite new. What I see here is it is the same implementation but two different outcomes, only difference is whether the Breadcrumbs::current()->title is called or not.

I can still render the breadcrumb if I remove the mentioned method, so pretty sure the model is passed and loaded correctly. So there's no need to load it again when defining the breadcrumb.

shengslogar commented 3 years ago

Unfortunately, you will need to load the model again within your breadcrumbs file unless you find some other way to share data between breadcrumbs and your controller.

As you have demonstrated with your dds, the same data being passed to your controller method is the exact same data being passed to the breadcrumb method. Since you switched to the repository method, both methods are getting an ID string instead of the model.

To make this more "Eloquent," you might consider customizing Laravel's route-model binding to work with your repository.

https://laravel.com/api/8.x/Illuminate/Database/Eloquent/Model.html#method_resolveRouteBinding

Documentation is sparse, but a few quick internet searches should get you on the right track.

Internally, I'm not sure how Laravel does its own route-model binding in terms of duplicate database requests (in this case, between breadcrumbs and your controller). I'd hope they do some sort of caching, but it's quite possible duplicate requests are happening anyway.

Hope that helps.