itstructure / laravel-multi-menu
Widget for rendering multilevel menu for Laravel framework
Requires
- php: >=7.1.0
- laravel/framework: ~5.5.0|~5.6.0|~5.7.0|~5.8.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ~3.5.0|~3.6.0|~3.7.0|~3.8.0|^4.0|^5.0|^6.0|^7.0|^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^6.5|^7.0|^8.0|^9.0|^10.0|^11.0|^12.0
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
1 Introduction
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.
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=configIt stores
multimenu.phpconfig file toconfigfolder. -
To publish only views run command:
php artisan multimenu:publish --only=viewsIt stores view files to
resources/views/vendor/multimenufolder. -
Else you can use
--forceargument 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
.envfile. 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
vendorfolder and thecomposer.lockfile 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
-
Get the package source code.
If you have git, run command:
git clone https://github.com/itstructure/laravel-multi-menu.gitIf 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.
-
Go to the package folder:
cd laravel-multi-menu -
Install dependencies (it is enough to do it once):
composer installComposer itself chooses the Laravel and PHPUnit versions, which fit your PHP version.
Note! PHP must have
pdo_sqliteandmbstringextensions. Usually they are already enabled. If not, Composer or the tests will tell you about it. -
Run the tests:
composer testOr the same directly:
- Linux / macOS:
vendor/bin/phpunit - Windows:
vendor\bin\phpunit
- Linux / macOS:
-
If you want to run only one test, add
--filterwith 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.
-
Get the package source code and go to the package folder, as in steps 1 and 2 of Way 1.
-
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 thevendorfolder will belong to you, not to therootuser. In Windows PowerShell it is not needed.-v "$PWD":/app- the current folder (package folder) is available inside the container as/app.
-
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_nameto 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.
-
Choose a pair of versions from the table:
PHP image Laravel Testbench php:7.1-cli5.5.*3.5.*php:7.2-cli5.8.*3.8.*php:7.2-cli6.*4.*php:7.3-cli7.*5.*php:7.3-cli8.*6.*php:8.0-cli9.*7.*php:8.1-cli10.*8.*php:8.2-cli11.*9.*php:8.3-cli12.*10.*php:8.3-cli13.*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. -
Go to the package folder. If you have already installed dependencies before, delete the
vendorfolder and thecomposer.lockfile. -
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.*and6.*to the values from the table. Forplatform.phpyou can write any version of the chosen PHP, for example7.1.33,8.0.30,8.2.0. -
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
-
Important! The command in step 3 changes the
composer.jsonfile of the package. When you finish, return it back and delete the installed files:git checkout composer.jsonAnd delete the
vendorfolder and thecomposer.lockfile.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
-
Your project tests and the package tests do not mix.
When you run tests of your project (
composer test,php artisan testorvendor/bin/phpunitin the project folder), only tests of your project are run. The projectphpunit.xmlfile points to the projecttestsfolder, and the package lies invendor/itstructure/laravel-multi-menu. So the package tests are not run there. -
Usually there are no package tests in your project at all.
By default Composer downloads the package as an archive. The
testsfolder, the.githubfolder and thephpunit.xml.distfile are excluded from this archive (see the.gitattributesfile), because they are not needed for the package to work. So invendor/itstructure/laravel-multi-menuthere is only the package itself. -
Exception: installation with
--prefer-source.If the package is installed with
--prefer-source(Composer makesgit cloneinstead 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.
-
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.
- If the package is installed from a local folder (
License
Copyright © 2018-2026 Andrey Girnik girnikandrey@gmail.com.
Licensed under the MIT license. See LICENSE.txt for details.
