Voyager: Can't Create new Dimmers

Created on 28 Jun 2018  路  12Comments  路  Source: the-control-group/voyager

  • Laravel: v5.5.40
  • Voyager: v1.1
  • PHP: 7.0.3
  • Database: MySQL Ver 14.14 Distrib 5.7.22,

Description

I can't create new Dimmers

What is the new way to create them?

question user error

Most helpful comment

Do you have set the right permissions on for the logged in user?

My custom dimmer looks like the following and it works:

<?php

namespace App\Widgets;

use App\Models\News;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Widgets\BaseDimmer;

class NewsDimmer extends BaseDimmer
{
    /**
     * The configuration array.
     *
     * @var array
     */
    protected $config = [];

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        $count =  News::count();
        $string = trans_choice('dimmer.news', $count);

        return view('voyager::dimmer', array_merge($this->config, [
            'icon'   => 'voyager-news',
            'title'  => "{$count} {$string}",
            'text'   => __('dimmer.news_text', ['count' => $count, 'string' => Str::lower($string)]),
            'button' => [
                'text' => __('dimmer.news_link_text'),
                'link' => route('voyager.news.index'),
            ],
            'image' => asset('images/paige_spiranac_golf.jpg'),
        ]));
    }

    /**
     * Determine if the widget should be displayed.
     *
     * @return bool
     */
    public function shouldBeDisplayed()
    {
        return Auth::user()->can('browse', app(News::class));
    }
}

All 12 comments

check configuration file located at config/voyager.php

widgets
Here you can manage the widgets that live on your dashboard. You can take a look at an example widget class by viewing the current widgets inside of tcg/voyager/src/Widgets.

https://voyager.readme.io/v1.1/docs/configurations

What I did
mkdir app/Widgets and

//voyager.php
'widgets' => [
            'TCG\\Voyager\\Widgets\\UserDimmer',
            'App\\Widgets\\BuildingDimmer',
            'App\\Widgets\\ClientDimmer',
            'App\\Widgets\\neighborhoodDimmer',
            'App\\Widgets\\PropertyDimmer',
            'App\\Widgets\\SuperintendentDimmer',
        ],
<?php
// app/Widgets/BuildingDimmer.php
namespace TCG\Voyager\Widgets;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Facades\Voyager;

class BuildingDimmer extends BaseDimmer
{
    /**
     * The configuration array.
     *
     * @var array
     */
    protected $config = [];

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        $count = Voyager::model('Building')->count();
        $string = trans_choice('voyager::dimmer.building', $count);

        return view('voyager::dimmer', array_merge($this->config, [
            'icon'   => 'voyager-company',
            'title'  => "{$count} {$string}",
            'text'   => __('voyager::dimmer.user_text', ['count' => $count, 'string' => Str::lower($string)]),
            'button' => [
                'text' => __('voyager::dimmer.user_link_text'),
                'link' => route('voyager.buildings.index'),
            ],
            'image' => voyager_asset('images/widget-backgrounds/01.jpg'),
        ]));
    }

    /**
     * Determine if the widget should be displayed.
     *
     * @return bool
     */
    public function shouldBeDisplayed()
    {
        Auth::user()->can('browse', Voyager::model('Building'));
    }
}

But I can't see my custom widgets. Only the User's Dimmer :(

Do you have set the right permissions on for the logged in user?

My custom dimmer looks like the following and it works:

<?php

namespace App\Widgets;

use App\Models\News;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use TCG\Voyager\Widgets\BaseDimmer;

class NewsDimmer extends BaseDimmer
{
    /**
     * The configuration array.
     *
     * @var array
     */
    protected $config = [];

    /**
     * Treat this method as a controller action.
     * Return view() or other content to display.
     */
    public function run()
    {
        $count =  News::count();
        $string = trans_choice('dimmer.news', $count);

        return view('voyager::dimmer', array_merge($this->config, [
            'icon'   => 'voyager-news',
            'title'  => "{$count} {$string}",
            'text'   => __('dimmer.news_text', ['count' => $count, 'string' => Str::lower($string)]),
            'button' => [
                'text' => __('dimmer.news_link_text'),
                'link' => route('voyager.news.index'),
            ],
            'image' => asset('images/paige_spiranac_golf.jpg'),
        ]));
    }

    /**
     * Determine if the widget should be displayed.
     *
     * @return bool
     */
    public function shouldBeDisplayed()
    {
        return Auth::user()->can('browse', app(News::class));
    }
}

@guillermoprojaslema, you've done ALMOST everything right. Just change the namespace in your dimmer class (should be App\Widgets, not the TCG namespace you have currently).

Now i got this error

"Class 'AppWidgetsBaseDimmer' not found"

:(

You have to add use TCG\Voyager\Widgets\BaseDimmer just like you would any class that isn't in the same namespace

@guillermoprojaslema that's only added since version 1.1.3 Before it extended AbstractWidget and the shouldBeDisplayed method was not required.

Thank you all guys.

Well, I wanna opene this thread again. In my workplace it works, but in my laptop it does not. I don't rememeber the enviroment from my workplace, but the one I posted first is the one that this does not work.
Also, The 'primary_color' => '#A10000', does not work.

/** * Determine if the widget should be displayed. * * @return bool */ public function shouldBeDisplayed() { return Auth::user()->can('browse', app(News::class)); }

Work fine, thx!

Thank you all. It works.

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.

Was this page helpful?
0 / 5 - 0 ratings