You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Oct 18, 2020. It is now read-only.
Hi, I added a very useful method in Laravel Breadcrumbs and use it since 1 year. It gives the ability to generate all GET breadcrumbs routes the same way Route::resource generates them. Here's the source code:
/**
* Generate the resource breadcrumbs
*
* @param string $prefix
* @param string $modelClass
* @param string $label
* @return void
*/
public static function resource(string $prefix, string $modelClass, $label = 'name')
{
$modelName = substr($modelClass, strrpos($modelClass, '\\') + 1);
$arr = preg_split('/(?=[A-Z])/', $modelName);
$lowerCaseModelName = null;
$firstWord = true;
foreach ($arr as $word) {
if ($word !== '') {
$wordToAdd = strtolower($word);
if (! $firstWord) {
$wordToAdd = '_' . $wordToAdd;
}
$lowerCaseModelName .= $wordToAdd;
$firstWord = false;
}
}
Breadcrumbs::register("{$prefix}.{$lowerCaseModelName}.index", function ($breadcrumbs) use ($prefix, $lowerCaseModelName) {
$breadcrumbs->parent("{$prefix}.index");
$breadcrumbs->push(ucfirst(str_plural($lowerCaseModelName)), route("{$prefix}.{$lowerCaseModelName}.index"));
});
Breadcrumbs::register("{$prefix}.{$lowerCaseModelName}.create", function ($breadcrumbs) use ($prefix, $lowerCaseModelName) {
$breadcrumbs->parent("{$prefix}.{$lowerCaseModelName}.index");
$breadcrumbs->push("Create", route("{$prefix}.{$lowerCaseModelName}.create"));
});
Breadcrumbs::register("{$prefix}.{$lowerCaseModelName}.edit", function ($breadcrumbs, $model) use ($prefix, $lowerCaseModelName, $modelClass, $label) {
$breadcrumbs->parent("{$prefix}.{$lowerCaseModelName}.index");
$breadcrumbs->push($model->$label, route("{$prefix}.{$lowerCaseModelName}.edit", $model->id));
});
Breadcrumbs::register("{$prefix}.{$lowerCaseModelName}.show", function ($breadcrumbs, $model) use ($prefix, $lowerCaseModelName, $modelClass, $label) {
$breadcrumbs->parent("{$prefix}.{$lowerCaseModelName}.index");
$breadcrumbs->push($model->$label, route("{$prefix}.{$lowerCaseModelName}.show", $model->id));
});
}
I think the method would be beneficial to all the community. What do you think?
The text was updated successfully, but these errors were encountered:
I think it makes too many assumptions about the way the routes are structured and models are named to put it in the core package - but thanks for sharing.
Hi, I added a very useful method in Laravel Breadcrumbs and use it since 1 year. It gives the ability to generate all GET breadcrumbs routes the same way Route::resource generates them. Here's the source code:
I think the method would be beneficial to all the community. What do you think?
The text was updated successfully, but these errors were encountered: