{note} You're browsing the documentation for an old version of laravel-breadcrumbs. Consider upgrading your project to v3.

Resourceful Controllers

Laravel automatically creates route names for resourceful controllers, e.g. photo.index, which you can use when defining your breadcrumbs. For example:

// routes/web.php
Route::resource('photo', PhotoController::class);

The generated routes:

$ php artisan route:list
+--------+----------+--------------------+---------------+-------------------------+------------+
| Domain | Method   | URI                | Name          | Action                  | Middleware |
+--------+----------+--------------------+---------------+-------------------------+------------+
|        | GET|HEAD | photo              | photo.index   | [email protected]   |            |
|        | GET|HEAD | photo/create       | photo.create  | [email protected]  |            |
|        | POST     | photo              | photo.store   | [email protected]   |            |
|        | GET|HEAD | photo/{photo}      | photo.show    | [email protected]    |            |
|        | GET|HEAD | photo/{photo}/edit | photo.edit    | [email protected]    |            |
|        | PUT      | photo/{photo}      | photo.update  | [email protected]  |            |
|        | PATCH    | photo/{photo}      |               | [email protected]  |            |
|        | DELETE   | photo/{photo}      | photo.destroy | [email protected] |            |
+--------+----------+--------------------+---------------+-------------------------+------------+

Your breadcrumbs:

// routes/breadcrumbs.php

// Photos
Breadcrumbs::for(
    'photo.index',
    fn (Generator $trail) => $trail->parent('home')->push('Photos', route('photo.index'))
);

// Photos > Upload Photo
Breadcrumbs::for(
    'photo.create',
    fn (Generator $trail) => $trail->parent('photo.index')->push('Upload Photo', route('photo.create'))
);

// Photos > [Photo Name]
Breadcrumbs::for(
    'photo.show',
    fn (Generator $trail, $photo) => $trail->parent('photo.index')->push($photo->title, route('photo.show', $photo))
);

// Photos > [Photo Name] > Edit Photo
Breadcrumbs::for(
    'photo.edit',
    fn (Generator $trail, $photo) => $trail->parent('photo.show', $photo)->push('Edit Photo', route('photo.edit', $photo))
);

For more details see Resource Controllers in the Laravel documentation.

← Previous Topic

Prepending Breadcrumbs

Next Topic →

Using Multiple Files

Caught a mistake? Suggest an edit on Github