Search code examples
phplaravelvoyager

Hide of widgets in voyager


I'm working in laravel 5.6 with Voyager and I want to implement the widgets of the voyager but however it's showing me all the widgets I have Created but I need to hide some widgets from normal users as some widgets only accessible by Admin.I'm not able to hide the widgets it showing all widgets.. Thanks in advance.

public function run()
{

     $count = \App\Organization::count();


    $string = trans_choice('Organization', $count);


    return view('voyager::dimmer', array_merge($this->config, [
        'icon' => 'voyager-edit',
        'title' => [
            'text' => "{$string} {$count}",
            'link' => route('org_data'),
        ],
        // 'image' => voyager_asset('images/widget-backgrounds/03.jpg'),
    ]));
}

Solution

  • you need to change widget's shouldBeDisplayed method as following:

    public function shouldBeDisplayed(){
       return auth()->user()->hasRole('admin');
    }
    

    it simply checks wether current user's role is admin or not.

    good luck