Skip to content

Instantly share code, notes, and snippets.

@nasrulhazim
Last active August 21, 2016 16:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nasrulhazim/f15dfc23b750c27bfd6c8f4770e13b43 to your computer and use it in GitHub Desktop.
Save nasrulhazim/f15dfc23b750c27bfd6c8f4770e13b43 to your computer and use it in GitHub Desktop.
Array Multidimensional Unique
<?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
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'
]
]
];

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
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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment