Laravel-adminlte: array_push() expects at least 2 parameters, 1 given

Created on 12 Jul 2020  路  5Comments  路  Source: jeroennoten/Laravel-AdminLTE

| Question | Answer
| ----------------------- | -----------------------
| Issue or Enhancement | Issue
| Laravel Version | 7.18.1
| Project Version | ^3.4

I get an error array_push() expects at least 2 parameters, 1 given (View: ...\resources\views\vendor\adminlte\partials\navbar\navbar.blade.php) when I add the code 'permission' => 'admin', in $event->menu->add[] array

All 5 comments

Hi @eraporsmk , are you using Laratrust? If you not using Laratrust please give us a detailed example of how you are adding the menu items.

If you using Laratrust and your custom menu filter is like next one:

<?php

namespace MyApp;

use JeroenNoten\LaravelAdminLte\Menu\Filters\FilterInterface;
use Laratrust\Laratrust;

class MyMenuFilter implements FilterInterface
{
    public function transform($item)
    {
        if (isset($item['permission']) && ! Laratrust::isAbleTo($item['permission'])) {
            return false;
        }

        return $item;
    }
}

Please, try the next one instead:

<?php

namespace MyApp;

use JeroenNoten\LaravelAdminLte\Menu\Filters\FilterInterface;
use Laratrust\Laratrust;

class MyMenuFilter implements FilterInterface
{
    public function transform($item)
    {
        if (isset($item['permission']) && ! Laratrust::isAbleTo($item['permission'])) {
            $item['restricted'] = true;
        }

        return $item;
    }
}

If that solves your issue, we just need to update the example on the README.

Hi @Shidersz, yes i'm using Laratrust.

Here my filter code:

<?php

namespace App;

use JeroenNoten\LaravelAdminLte\Menu\Filters\FilterInterface;
use Laratrust\Laratrust;

class MyMenuFilter implements FilterInterface
{
    public function transform($item)
    {
        if (isset($item['permission']) && ! Laratrust::hasRole($item['permission'])) {
            $item['restricted'] = true;
        }
        return $item;
    }
}

but I still get an error array_push().
if i'am edit src/Menu/Builder.php line 46

Before:

array_push($this->menu, ...$items)`

After:

($items) ? array_push($this->menu, ...$items) : [];

I did not find an error again

Sorry for my bad english :)
Thanks for your reply

Hi @eraporsmk thanks for your feedback. Please, try changing the next method to the new version:

OLD Version

/**
 * Add new items at the end of the menu.
 *
 * @param mixed $newItems Items to be added
 */
public function add(...$newItems)
{
    $items = $this->transformItems($newItems);
    array_push($this->menu, ...$items);
}

NEW Version

/**
 * Add new items at the end of the menu.
 *
 * @param mixed $newItems Items to be added
 */
public function add(...$newItems)
{
    $items = $this->transformItems($newItems);

    if (! empty($items)) {
        array_push($this->menu, ...$items);
    }
}

I believe the problem is caused because all the items you are adding are not allowed for the current user. Do the new version works on your environment??

Hi @Shidersz, I added menus for several roles and I use the NEW Version of you.
The new version works well in my environment

Sorry for my bad english :)
Thanks for your reply

fixed with release v3.4.3

Was this page helpful?
0 / 5 - 0 ratings

Related issues

santiagoferraz picture santiagoferraz  路  5Comments

Shidersz picture Shidersz  路  5Comments

Basili0 picture Basili0  路  5Comments

samuelm77 picture samuelm77  路  4Comments

niltonssjr picture niltonssjr  路  4Comments