Skip to main content

Overview

The Menu model represents a navigation menu container in LaraCMS. Each menu can contain multiple menu items organized in a hierarchical structure. This allows you to create different menus for various parts of your site (e.g., header navigation, footer links, sidebar). Namespace: App\Models\Menu File: app/Models/Menu.php:7

Properties

Fillable attributes

name
string
required
The name/identifier of the menu (e.g., “primary”, “footer”, “mobile”)

Relationships

items()

Has many relationship with the MenuItem model. Items are automatically ordered by their order field. Returns: HasMany<MenuItem> Default ordering: By order column (ascending)
// Get all menu items in order
$menu = Menu::find(1);
$items = $menu->items; // Already ordered

// Get only top-level items (no parent)
$topLevelItems = $menu->items()->whereNull('parent_id')->get();

// Eager load items with children
$menu = Menu::with('items.children')->find(1);

Usage examples

Creating a menu

$menu = Menu::create([
    'name' => 'primary'
]);

Adding items to a menu

$menu = Menu::where('name', 'primary')->first();

// Add top-level item
$menu->items()->create([
    'label' => 'Home',
    'url' => '/',
    'order' => 1
]);

// Add item with children
$aboutItem = $menu->items()->create([
    'label' => 'About',
    'url' => '/about',
    'order' => 2
]);

// Add child items
$aboutItem->children()->create([
    'menu_id' => $menu->id,
    'label' => 'Team',
    'url' => '/about/team',
    'order' => 1
]);

Rendering a menu

// In controller
$primaryMenu = Menu::where('name', 'primary')
    ->with(['items' => function($query) {
        $query->whereNull('parent_id')->with('children');
    }])
    ->first();

return view('layouts.navigation', compact('primaryMenu'));
<!-- In Blade template -->
@if($primaryMenu)
<nav>
    <ul>
        @foreach($primaryMenu->items as $item)
            <li>
                <a href="{{ $item->url }}">{{ $item->label }}</a>
                
                @if($item->children->count() > 0)
                    <ul>
                        @foreach($item->children as $child)
                            <li>
                                <a href="{{ $child->url }}">{{ $child->label }}</a>
                            </li>
                        @endforeach
                    </ul>
                @endif
            </li>
        @endforeach
    </ul>
</nav>
@endif

Querying menus

// Get menu by name
$menu = Menu::where('name', 'primary')->first();

// Get all menus with item counts
$menus = Menu::withCount('items')->get();

// Check if menu exists
if (Menu::where('name', 'footer')->exists()) {
    // Menu exists
}

Database schema

Based on migration 2025_05_25_175641_create_menus_table.php, the menus table contains:
  • id - Primary key
  • name - Menu identifier (string)
  • created_at - Timestamp
  • updated_at - Timestamp
Common menu locations in LaraCMS:
  • primary - Main header navigation
  • footer - Footer links
  • mobile - Mobile menu
  • sidebar - Sidebar navigation
Use descriptive menu names that indicate their location or purpose to make template development easier.

MenuItem model

View MenuItem model documentation

Menu feature

Learn about the menu management feature

Build docs developers (and LLMs) love