nasrulhazim
8/21/2016 - 4:16 PM

Array Multidimensional Unique

Array Multidimensional Unique

If a user has multiple roles, same main menu won't be duplicate - see superadministrator and administrator, having the same menu.

array:2 [▼
  0 => array:2 [▼
    "name" => "Users Manager"
    "route" => "users.index"
  ]
  1 => array:2 [▼
    "name" => "Acl Manager"
    "route" => "acls.index"
  ]
]
<?php

return [
	'superadministrator' => [
		[
			'name' => 'Users Manager',
			'route' => 'users.index'
		],
		[
			'name' => 'Acl Manager',
			'route' => 'acls.index'
		],
	],
	'administrator' => [
		[
			'name' => 'Users Manager',
			'route' => 'users.index'
		]
	],
	'manager' => [
		[
			'name' => 'Seminars Manager',
			'route' => 'seminars.index'
		],
		[
			'name' => 'Event Manager',
			'route' => 'events.index'
		],
		[
			'name' => 'Attendance Manager',
			'route' => 'attendances.index'
		],
		[
			'name' => 'Organiser Manager',
			'route' => 'organisers.index'
		],
		[
			'name' => 'Survey Manager',
			'route' => 'surveys.index'
		]
	],
	'management' => [
		[
			'name' => 'Seminars Manager',
			'route' => 'seminars.index'
		],
		[
			'name' => 'Event Manager',
			'route' => 'events.index'
		],
		[
			'name' => 'Attendance Manager',
			'route' => 'attendances.index'
		],
		[
			'name' => 'Organiser Manager',
			'route' => 'organisers.index'
		],
		[
			'name' => 'Survey Manager',
			'route' => 'surveys.index'
		],
		[
			'name' => 'Report Manager',
			'route' => 'reports.index'
		]
	],
	'participant' => [
		[
			'name' => 'Profile',
			'route' => 'profile'
		]
	]
];
<?php

if(! function_exists('array_multiunique')) {
	function array_multiunique($val) {
		$serialized_array = array_map("serialize", $val);

        foreach ($serialized_array as $key => $val) {
             $result[$val] = true;
        }

        $result = array_map("unserialize", (array_keys($result)));

        return $result;
	}
}
<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use App\Repositories\UserRepository;
use Auth;

class MenuComposer
{
    /**
     * Create a new profile composer.
     *
     * @param  UserRepository  $menus
     * @return void
     */
    public function __construct()
    {
        // Dependencies automatically resolved by service container...
        
    }

    /**
     * Bind data to the view.
     *
     * @param  View  $view
     * @return void
     */
    public function compose(View $view)
    {
        $menus = [];
        if(!Auth::guest()) {
            // if has more than one roles, merge the menus
            $user = Auth::user();
            $roleuser = \App\RoleUser::where('user_id', $user->id)->get()->pluck('role_id');
            
            $roles = \App\Role::whereIn('id', $roleuser)->get()->pluck('name')->toArray();
            
            foreach ($roles as $key => $value) {
                $menus = array_merge(config('menus.'.$value), $menus);
            }

            $menus = array_multiunique($menus);
        }
        $view->with('menus', $menus);
    }
}