Closed joedawson closed 10 years ago
Thanks for the level of detail.
$video
contains the URL slug, not an object, because it takes the same parameters as the controller - specifically this line:
public function show($slug)
There are a few possible solutions to this:
Use route model binding to move the conversion from slug to object into app/routes.php
(recommended - this is what the docs assume). Since you're using slugs not IDs, you'll have to use Route::bind()
in place of Route::model()
:
Route::bind('videos', function($slug) {
$video = Video::where('slug', '=', $slug)->first();
if (!$video) throw new ModelNotFoundException;
return $video;
});
This way both the controller and breadcrumbs will receive a model object, $video
, instead of $slug
. (Note: I've never tried that with Route::resource()
, but the docs imply it will still work.)
Re-load the model in app/breadcrumbs.php
:
Breadcrumbs::register('videos.show', function($breadcrumbs, $slug) {
$video = Video::where('slug', '=', $slug)->first();
$breadcrumbs->parent('videos.index');
$breadcrumbs->push($video->title, route('videos.show', $video->slug));
});
(But that adds a redundant database call.)
Pass it from the view manually:
@section('breadcrumbs', Breadcrumbs::render('videos.show', $video))
Hope that helps.
Thanks for the explanation Dave, I went for the third solution. Appreciate your time :)
Hey Dave, I have similar issue. Is there a way to pass objects directly from controller to breadcrumbs. I don't want to pass model directly from routes, and also I can't pass it from view, because I have set bc::render in my default layout. Having if statement for every page is not an option..
Thanks very much
You might be able to access the view variables directly from the controller... I've not tried that though, depends how Laravel is set up.
Or you can use Breadcrumbs::setCurrentRoute($name, $param1, $param2...)
in the controller.
Hey, loving this package thus' far.
I've read through all docs, but unfortunately can't work out my issue.
app/routes.php
app/controllers/VideosController.php
app/views/videos/show.blade.php
app/views/layouts/single.blade.php
app/breadcrumbs.php - I actually read through #18 for my breadcrumbs boilerplate.
When I replace;
With:
$video
returns the 'slug'. Not throwing any errors, but rendering breadcrumbs like so:Providers
Aliases
Breadcrumbs Version: dev-master Laravel Version: 4.2.6 PHP Version: 5.5.12
I hope I've provided you with everything you need :+1: