Frontdesk simplifies the way you build a navigation bar using models within your Laravel application. Frontdesk treats a navigation menu like any other model, so you can have total, and dynamic control over the contents of your menus.
You can install the package via composer:
composer require plank/frontdesk
Frontdesk separates the concept of a navigation bar into 2 parts:
the Menu
and the Hyperlink
.
A Menu
is a collection of Hyperlink
s.
Each Hyperlink
can have a parent Hyperlink
and a collection of child Hyperlink
s.
To that end you may have content that is "linkable" and content that is "menuable".
To use Frontdesk simply add the traits and implement the corresponding interfaces on your models.
class MyModel extends Model implements Linkable
{
use IsLinkable;
public function linkTitle(): Attribute
{
return Attribute::make(
get: fn () => $this->title
);
}
public function linkUrl(): Attribute
{
return Attribute::make(
get: fn () => route('my-model.show', $this)
);
}
}
class MyMenuModel extends Model implements Menuable
{
use HasMenus;
}
Once you have a few models that implement the appropriate interfaces you can start building your navigation bar.
// Create a menu
$myMenu = MyMenuModel::find(1)->menus()->create([
'identifier' => 'header-nav'
]);
$myOtherMenu = MyMenuModel::find(1)->menus()->create([
'identifier' => 'footer-nav'
]);
// Create a hyperlink referencing
$myModelLink = MyModel::find(1)->hyperlinks()->create([
'menu_id' => $myMenu->id,
]);
// A link also doesn't strictly need to be attached to a model
$myMenuLink = Hyperlink::create([
'menu_id' => $myMenu->id,
'title' => 'My Link',
'url' => 'https://example.com',
]);
// You can also associate an existing hyperlink to an existing menu
$myMenuLink->menus()->associate($myOtherMenu)->save();
After building a few menus, you can retrieve them using the Menu
model, or via a model's relation to the Menu
model.
// Get a menu by identifier
$myMenu = Menu::where('identifier', 'header-nav')->first();
// Via a model relationship
$myMenu = MyMenuModel::find(1)->menus()->where('identifier', 'header-nav')->first();
Getting links out of the menu is as simple as calling the hyperlinks
relationship on the Menu
model.
$myMenu->hyperlinks;
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.