Voyager: Trying to show a setting of type image lacks the storage/ prefix

Created on 22 Jan 2018  路  3Comments  路  Source: the-control-group/voyager

  • Laravel Version: 5.5.32
  • Voyager Version: 1.0.14
  • PHP Version: 7.1.7
  • Database Driver & Version: MySQL 5.7

Description:

Trying to show an image that is saved as a setting. setting('key.value').
It should have the storage/ prefix. Instead its path starts from settings/....

Steps To Reproduce:

Create an image setting and try to show it on your front-end. Using HTTPS.

I have altered the code like so:
File TCG\Voyager\Voyager.php

    public function setting($key, $default = null)
    {
        if ($this->setting_cache === null) {
            foreach (self::model('Setting')->all() as $setting) {
                $keys = explode('.', $setting->key);

               // Lines added start here
               if($setting->type == 'image') {
                    $setting->value = 'storage/' . $setting->value;
                }
               // Lines added end here

                @$this->setting_cache[$keys[0]][$keys[1]] = $setting->value;
            }
        }

        $parts = explode('.', $key);

        if (count($parts) == 2) {
            return @$this->setting_cache[$parts[0]][$parts[1]] ?: $default;
        } else {
            return @$this->setting_cache[$parts[0]] ?: $default;
        }
    }

so it will properly show the image.

I ain't sure if that's the proper solution though.

Most helpful comment

You have to use:
{{ Voyager::image(setting('key.value')) }}
This will return an absolute path

All 3 comments

This solution will affect the admin logo and loader images from loading.

You will need to change these too:

    <?php $admin_loader_img = Voyager::setting('admin.loader', ''); ?>
    @if($admin_loader_img == 'storage/')
        <img src="{{ voyager_asset('images/logo-icon.png') }}" alt="Voyager Loader">
    @else
        <img src="{{ Voyager::image($admin_loader_img) }}" alt="Voyager Loader">
    @endif
    <?php $admin_logo_img = Voyager::setting('admin.icon_image', ''); ?>
    @if($admin_logo_img == 'storage/')
        <img src="{{ voyager_asset('images/logo-icon-light.png', true) }}" alt="Logo Icon">
    @else
        <img src="{{ Voyager::image($admin_logo_img) }}" alt="Logo Icon">
    @endif

You have to use:
{{ Voyager::image(setting('key.value')) }}
This will return an absolute path

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

Related issues

MikadoInfo picture MikadoInfo  路  3Comments

iwasherefirst2 picture iwasherefirst2  路  3Comments

saintmim picture saintmim  路  4Comments

Nagendra1421 picture Nagendra1421  路  3Comments

abacram picture abacram  路  3Comments