Search by

itstructure / laravel-multi-menu

itstructure

Widget for rendering multilevel menu for Laravel framework

Package info

github.com/itstructure/laravel-multi-menu

pkg:composer/itstructure/laravel-multi-menu

Statistics

Installs: 245

Dependents: 0

Suggesters: 0

Stars: 4

Open Issues: 0

2.1.0 2026-12-02 16:48 UTC

This package is auto-updated.

Last update: 2026-12-02 16:48:34 UTC


README

1 Introduction

Latest Stable Version Latest Unstable Version License Total Downloads Build Status Scrutinizer Code Quality Tests

This widget is to display a multi level menu. There can be nested sub-menus. Used for Laravel framework.

The widget uses data from the database, in which there are, in addition to the primary keys, also the parent keys.

Data from the database is taken from a model and must be instance of Illuminate\Database\Eloquent\Collection.

Multi level menu example scheme

2 Requirements

  • laravel 5.5+ | 6+ | 7+ | 8+ | 9+ | 10+ | 11+ | 12+ | 13+
  • php >= 7.1.0
  • composer

3 Installation

3.1 General from remote repository

Via composer:

composer require itstructure/laravel-multi-menu "^2.1.0"

3.2 If you are testing this package from local server directory

In application composer.json file set the repository, like in example:

"repositories": [
    {
        "type": "path",
        "url": "../laravel-multi-menu",
        "options": {
            "symlink": true
        }
    }
],

Here,

../laravel-multi-menu - directory name, which has the same directory level as application and contains multi menu package.

Then run command:

composer require itstructure/laravel-multi-menu:dev-master --prefer-source

3.3 Publish in application

  • To publish all parts run command:

    php artisan multimenu:publish

  • To publish only config run command:

    php artisan multimenu:publish --only=config

    It stores multimenu.php config file to config folder.

  • To publish only views run command:

    php artisan multimenu:publish --only=views

    It stores view files to resources/views/vendor/multimenu folder.

  • Else you can use --force argument to rewrite already published files.

Or another variant:

php artisan vendor:publish --provider="Itstructure\MultiMenu\MultiMenuServiceProvider"

4 Usage

4.1 Simple variant

Config part

return [
    'primaryKeyName' => 'id', // Editable
    'parentKeyName' => 'parent_id', // Editable
    'mainTemplate' => 'main', // Editable
    'itemTemplate' => 'item', // Editable
];

View template part

@php
$multiOptions = [ // Editable
    'config' => config('multimenu'),
    'data' => $pages
];
@endphp
@multiMenu($multiOptions)

Here, $pages - is from controller part, for example $pages = Page::all();. Must be instance of Illuminate\Database\Eloquent\Collection.

Records with an empty parent key (NULL or 0) are the root menu items. If the collection has no root records (for example, only child records are given), the menu is empty.

4.2 Addition config options and data

Config part

There is an example to set item blade templates for 3 levels:

return [
    'primaryKeyName' => 'id',
    'parentKeyName' => 'parent_id',
    'mainTemplate' => 'main',
    'itemTemplate' => [
        'levels' => [
            'item',
            'item',
            'item_new',
        ]
    ],
];

You can set mainTemplate by analogy.

Blade templates

Example of a custom changed blade template file item.blade:

<li>
    <a href="{{ $data->icon }}">
        Initial item Id {{ $data->id }} {{ isset($addition) ? ' | ' . $addition : '' }}
    </a>
</li>

Example of a custom changed blade template file item_new.blade:

<li>
    <a href="{{ $data->icon }}" style="color: green; font-weight: bold;">
        New item Id {{ $data->id }} {{ isset($addition) ? ' | ' . $addition : '' }}
    </a>
</li>

Addition data

Example in a template file:

@php
$multiOptions = [
    'config' => config('multimenu'),
    'data' => $pages,
    'additionData' => [
        'levels' => [
            0 => [],
            1 => ['addition' => 'addition string']
        ]
    ]
];
@endphp
@multiMenu($multiOptions)

4.3 Database table structure example

Table "catalogs"

| id  | parent_id |   title    | ... |
|-----|-----------|------------|-----|
|  1  |   NULL    |   item 1   | ... |
|  2  |   NULL    |   item 2   | ... |
|  3  |   NULL    |   item 3   | ... |
|  4  |   NULL    |   item 4   | ... |
|  5  |   NULL    |   item 5   | ... |
|  6  |     2     |  item 2.1  | ... |
|  7  |     2     |  item 2.2  | ... |
|  8  |     7     | item 2.2.1 | ... |
|  9  |     7     | item 2.2.2 | ... |
|  10 |     7     | item 2.2.3 | ... |
| ... |    ...    |     ...    | ... |

5 Prevention of collisions

5.1 Before save model

To prevent the entry of the wrong parent identifier (for example, the new number that is a child in a subordinate chain of nested records):

Use static method checkNewParentId(Model $mainModel, int $newParentId... e.t.c)

Here are the required parameters:

$mainModel - current model record, in which the parent id will be changed for new value.

$newParentId - new parent id, which must be verified.

The method returns false, if the new parent is the record itself, any record of its sub chain, or a record which does not exist.

5.2 After delete model

To prevent breaks in the chain of subject submissions:

Use static method afterDeleteMainModel(Model $mainModel... e.t.c)

Here is the required parameter:

$mainModel - deleted model record.

This function will rebuild the chain.

Tests

The package has its own tests. They check menu rendering by levels, templates and addition data for levels, the @multiMenu directive, checkNewParentId(), afterDeleteMainModel() and the publish command.

Good to know before you start:

  • The tests do not need a database server and do not need a .env file. They use SQLite in memory, everything is created and deleted automatically.
  • The tests work on all supported versions: PHP 7.1 ... 8.5 and Laravel 5.5 ... 13.
  • When you install dependencies of the package, the vendor folder and the composer.lock file appear in the package folder. It is normal. They are in .gitignore, so they will not get into git.

Below there are several ways to run the tests. Choose the one, which fits you.

Way 1. You have PHP and Composer on your computer

  1. Get the package source code.

    If you have git, run command:

    git clone https://github.com/itstructure/laravel-multi-menu.git

    If you do not have git, open https://github.com/itstructure/laravel-multi-menu, press the green Code button, then Download ZIP, and unpack the archive.

  2. Go to the package folder:

    cd laravel-multi-menu

  3. Install dependencies (it is enough to do it once):

    composer install

    Composer itself chooses the Laravel and PHPUnit versions, which fit your PHP version.

    Note! PHP must have pdo_sqlite and mbstring extensions. Usually they are already enabled. If not, Composer or the tests will tell you about it.

  4. Run the tests:

    composer test

    Or the same directly:

    • Linux / macOS: vendor/bin/phpunit
    • Windows: vendor\bin\phpunit
  5. If you want to run only one test, add --filter with the test name:

    vendor/bin/phpunit --filter testCheckNewParentId

If everything is fine, you will see a message like: OK (14 tests, 29 assertions).

Way 2. You have nothing except Docker

You do not need to install PHP and Composer. Docker downloads them by itself.

  1. Get the package source code and go to the package folder, as in steps 1 and 2 of Way 1.

  2. Install dependencies (it is enough to do it once).

    For Linux, macOS and WSL terminal:

    docker run --rm --user "$(id -u):$(id -g)" -e COMPOSER_HOME=/tmp/composer -v "$PWD":/app -w /app composer:2 composer update --no-interaction

    For Windows PowerShell:

    docker run --rm -v "${PWD}:/app" -w /app composer:2 composer update --no-interaction

    Here:

    • --user "$(id -u):$(id -g)" - files in the vendor folder will belong to you, not to the root user. In Windows PowerShell it is not needed.
    • -v "$PWD":/app - the current folder (package folder) is available inside the container as /app.
  3. Run the tests.

    For Linux, macOS and WSL terminal:

    docker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/app -w /app php:8.4-cli php vendor/bin/phpunit

    For Windows PowerShell:

    docker run --rm -v "${PWD}:/app" -w /app php:8.4-cli php vendor/bin/phpunit

    To run only one test, add --filter test_name to the end of the command.

Note! In step 2 Composer installs the newest versions of Laravel and PHPUnit. They need a new PHP, so in step 3 use php:8.4-cli or newer. To check an old PHP or Laravel version, see Way 3.

Way 3. Check a specific PHP and Laravel version with Docker

It is useful, when you want to make sure that the package works for example on PHP 7.3 with Laravel 8.

  1. Choose a pair of versions from the table:

    PHP image Laravel Testbench
    php:7.1-cli 5.5.* 3.5.*
    php:7.2-cli 5.8.* 3.8.*
    php:7.2-cli 6.* 4.*
    php:7.3-cli 7.* 5.*
    php:7.3-cli 8.* 6.*
    php:8.0-cli 9.* 7.*
    php:8.1-cli 10.* 8.*
    php:8.2-cli 11.* 9.*
    php:8.3-cli 12.* 10.*
    php:8.3-cli 13.* 11.*

    Testbench (orchestra/testbench) is a tool for testing Laravel packages. Each Laravel version needs its own Testbench version, that is why it is in the table.

  2. Go to the package folder. If you have already installed dependencies before, delete the vendor folder and the composer.lock file.

  3. Install dependencies for the chosen pair. Example for PHP 7.3 + Laravel 8 (Linux, macOS, WSL terminal):

    docker run --rm --user "$(id -u):$(id -g)" -e COMPOSER_HOME=/tmp/composer -v "$PWD":/app -w /app composer:2 sh -c "composer config platform.php 7.3.33 && composer config policy.advisories.block false && composer require --no-update 'laravel/framework:8.*' && composer require --dev --no-update 'orchestra/testbench:6.*' && composer update --no-interaction --no-plugins"

    What this command does:

    • composer config platform.php 7.3.33 - tells Composer to choose packages for PHP 7.3, because Composer itself works on a newer PHP inside its container.
    • composer config policy.advisories.block false - allows installing old Laravel versions. New Composer refuses to install versions with known security advisories. It is OK for running tests, but never do it in a real project.
    • composer require --no-update ... - sets the chosen Laravel and Testbench versions.
    • --no-plugins - old Laravel versions bring Composer plugins, which are not needed for tests and are blocked by new Composer.

    For another pair, change 7.3.33, 8.* and 6.* to the values from the table. For platform.php you can write any version of the chosen PHP, for example 7.1.33, 8.0.30, 8.2.0.

  4. Run the tests with the PHP image from the table:

    docker run --rm --user "$(id -u):$(id -g)" -v "$PWD":/app -w /app php:7.3-cli php vendor/bin/phpunit
  5. Important! The command in step 3 changes the composer.json file of the package. When you finish, return it back and delete the installed files:

    git checkout composer.json

    And delete the vendor folder and the composer.lock file.

    If you downloaded the package as ZIP (without git), just download and unpack it again.

Way 4. Automatic tests on GitHub

You do not need to do anything. After each push and each pull request, GitHub runs the tests by itself on all version pairs from the table above (and a few more).

Results are on the Actions tab of the repository: https://github.com/itstructure/laravel-multi-menu/actions

Green check mark - all tests passed. Red cross - something is broken, open it to see the details.

The settings are in the .github/workflows/tests.yml file.

If the package is installed in your Laravel project

  1. Your project tests and the package tests do not mix.

    When you run tests of your project (composer test, php artisan test or vendor/bin/phpunit in the project folder), only tests of your project are run. The project phpunit.xml file points to the project tests folder, and the package lies in vendor/itstructure/laravel-multi-menu. So the package tests are not run there.

  2. Usually there are no package tests in your project at all.

    By default Composer downloads the package as an archive. The tests folder, the .github folder and the phpunit.xml.dist file are excluded from this archive (see the .gitattributes file), because they are not needed for the package to work. So in vendor/itstructure/laravel-multi-menu there is only the package itself.

  3. Exception: installation with --prefer-source.

    If the package is installed with --prefer-source (Composer makes git clone instead of downloading an archive), or from a local folder (see "If you are testing this package from local server directory" above), then the whole repository is in the project, the tests too.

    It does not break anything: your project tests still do not run them.

  4. How to run the package tests in this case.

    Do not run them through your project, the project has no tools needed for the package tests.

    Run them in the package folder itself, as described in Way 1 or Way 2:

    • If the package is installed from a local folder ("type": "path"), go to that local folder, for example ../laravel-multi-menu.
    • Otherwise, just clone the package repository to a separate folder.

License

Copyright © 2018-2026 Andrey Girnik girnikandrey@gmail.com.

Licensed under the MIT license. See LICENSE.txt for details.