PHPackages                             aslnbxrz/menu-builder - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. aslnbxrz/menu-builder

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

aslnbxrz/menu-builder
=====================

This is my package menu-builder

12[3 PRs](https://github.com/aslnbxrz/menu-builder/pulls)PHPCI passing

Since Jan 9Pushed 2mo agoCompare

[ Source](https://github.com/aslnbxrz/menu-builder)[ Packagist](https://packagist.org/packages/aslnbxrz/menu-builder)[ RSS](/packages/aslnbxrz-menu-builder/feed)WikiDiscussions main Synced today

READMEChangelogDependenciesVersions (4)Used By (0)

Menu Builder
============

[](#menu-builder)

[![Latest Version on Packagist](https://camo.githubusercontent.com/e2d9801e6e9f10c5474d44d78fe95ea408ac8e9201fbdfd3e3f6b2dcef25d57e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f61736c6e6278727a2f6d656e752d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aslnbxrz/menu-builder)[![GitHub Tests Action Status](https://camo.githubusercontent.com/ec3b1a4dcdaca02b8ed51bd4701c66187563d638aa900b3be80532a643283b49/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61736c6e6278727a2f6d656e752d6275696c6465722f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/aslnbxrz/menu-builder/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/15d3175c0ca65513fe4801dc1eb0f069502544a96340ca11ad8217da0e1229f1/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f61736c6e6278727a2f6d656e752d6275696c6465722f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/aslnbxrz/menu-builder/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/f99a12dccec59367ce2d25cd809259b923273f19281df6579034d97a04a3c3ee/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f61736c6e6278727a2f6d656e752d6275696c6465722e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/aslnbxrz/menu-builder)

Powerful and flexible menu builder package for Laravel applications. Create dynamic, hierarchical menus with support for permissions, routes, features, and more.

Features
--------

[](#features)

- 🎯 **Hierarchical Menu Structure** - Create unlimited nested menu items
- 🔐 **Permission-based Visibility** - Show/hide menu items based on user permissions
- 🛣️ **Route Validation** - Automatically validate route existence
- ⚡ **Built-in Caching** - High-performance caching for menu trees
- 🔗 **MorphTo Relationships** - Link menu items to any Eloquent model
- 🎨 **Multiple Menu Types** - Support for URL, Route, Permission, Feature, and Divider types
- 🗄️ **Database Agnostic** - Works with PostgreSQL, MySQL, and SQLite

Installation
------------

[](#installation)

You can install the package via composer:

```
composer require aslnbxrz/menu-builder
```

The package will automatically register its service provider.

### Publish Migrations

[](#publish-migrations)

Publish and run the migrations:

```
php artisan vendor:publish --tag="menu-builder-migrations"
php artisan migrate
```

This will create two tables:

- `menus` - Stores menu definitions
- `menu_items` - Stores menu items with hierarchical structure

**Note:** By default, `title` and `description` fields are **string** type. If you need multilingual support, see the [Multilingual Support](#multilingual-support-with-spatie-translatable) section below.

### Publish Config File

[](#publish-config-file)

You can publish the config file to customize table names and cache settings:

```
php artisan vendor:publish --tag="menu-builder-config"
```

This will create `config/menu-builder.php`:

```
return [
    'menuable' => [
        'field' => 'id',
    ],

    'menu' => [
        'table' => 'menus',
    ],

    'menu_item' => [
        'table' => 'menu_items',
    ],

    'cache' => [
        'key' => 'menu:tree:',
        'ttl' => 360, // minutes
    ],
];
```

Quick Start
-----------

[](#quick-start)

1. **Install the package:**

```
composer require aslnbxrz/menu-builder
```

2. **Publish and run migrations:**

```
php artisan vendor:publish --tag="menu-builder-migrations"
php artisan migrate
```

3. **Create your first menu:**

```
use Aslnbxrz\MenuBuilder\Models\Menu;
use Aslnbxrz\MenuBuilder\Models\MenuItem;
use Aslnbxrz\MenuBuilder\Enums\MenuItemType;

$menu = Menu::create([
    'alias' => 'main-menu',
    'title' => 'Main Menu',
    'is_active' => true,
]);

MenuItem::create([
    'menu_id' => $menu->id,
    'title' => 'Home',
    'link' => '/',
    'type' => MenuItemType::Url,
    'sort' => 1,
    'is_active' => true,
]);
```

4. **Display menu in your view:**

```
use Aslnbxrz\MenuBuilder\Facades\MenuBuilder;

$tree = MenuBuilder::getTree('main-menu', auth()->user());
```

Multilingual Support with Spatie Translatable
---------------------------------------------

[](#multilingual-support-with-spatie-translatable)

By default, the package uses **string** columns for `title` and `description` fields. If you need multilingual support, you can extend the models and use [Spatie Laravel Translatable](https://github.com/spatie/laravel-translatable) package.

#### Step 1: Install Spatie Translatable

[](#step-1-install-spatie-translatable)

```
composer require spatie/laravel-translatable
```

#### Step 2: Publish and Modify Migrations

[](#step-2-publish-and-modify-migrations)

Publish the migrations and change string to JSON:

```
php artisan vendor:publish --tag="menu-builder-migrations"
```

**In `create_menus_table.php`:**

```
// Change from:
$table->string('title')->nullable();
$table->text('description')->nullable();

// To:
$table->json('title')->nullable();
$table->json('description')->nullable();
```

**In `create_menu_items_table.php`:**

```
// Change from:
$table->string('title')->nullable();

// To:
$table->json('title')->nullable();
```

#### Step 3: Extend Models with Translatable

[](#step-3-extend-models-with-translatable)

Create your own models that extend the package models:

**`app/Models/Menu.php`:**

```
