voyager-themes icon indicating copy to clipboard operation
voyager-themes copied to clipboard

Undefined property $id when access themes option

Open henryonsoftware opened this issue 6 years ago • 4 comments

I got this error when access to themes option: Undefined property: stdClass::$id (View: /home/vagrant/code/test/vendor/tcg/voyager/resources/views/formfields/image.blade.php)

After 1 hour deep dive into Voyager Admin source I found the problem is Voyager Admin updated file image.blade.php at line 5, they want get $dataTypeContent->id But helpers.php missing it at line 17 $dataTypeContent = (object)[$key => $content];.

I tried change it to $dataTypeContent = (object)["id" => 0, $key => $content]; and it worked.

I don't know how to fix it, I just report issue and hope you guys fix it as correct way. Thanks!

henryonsoftware avatar Apr 07 '19 11:04 henryonsoftware

For an update-safe fix you can create a new ServiceProvider which includes a helper.php containing a fixed Version.

// app/Providers/Fixes/VoyagerThemeFixProvider.php

namespace App\Providers\Fixes;

use Illuminate\Support\ServiceProvider;

final class VoyagerThemeFixProvider extends ServiceProvider
{
    public function register()
    {
        include 'helper.php';
    }
}
// app/Providers/Fixes/helper.php

if(!function_exists('theme_field')) {
    function theme_field($type, $key, $title, $content = '', $details = '', $placeholder = '', $required = 0){

        $theme = \VoyagerThemes\Models\Theme::where('active', 1)->first();

        $option_exists = $theme->options->where('key', '=', $key)->first();

        $id = '';
        if(isset($option_exists->value)){
            $content = $option_exists->value;
            $id = $option_exists->id;
        }

        $row = (object)['required' => $required, 'field' => $key, 'type' => $type, 'details' => $details, 'display_name' => $placeholder];
        $dataTypeContent = (object)['id' => $id,$key => $content];
        $label = '<label for="'. $key . '">' . $title . '<span class="how_to">You can reference this value with <code>theme(\'' . $key . '\')</code></span></label>';
        $details = '<input type="hidden" value="' . $details . '" name="' . $key . '_details__theme_field">';
        $type = '<input type="hidden" value="' . $type . '" name="' . $key . '_type__theme_field">';
        return $label . app('voyager')->formField($row, '', $dataTypeContent) . $details . $type . '<hr>';
    }
}

Make sure you register this ServiceProvider before the WaveServiceProvider.

This also fixes the error

Padrio avatar Apr 15 '19 05:04 Padrio

Yes, thanks for your solution. I don't know about WaveServiceProvider, I put the fix provider after VoyagerServiceProvider and it worked.

I created a fix-bug PR but I think this repository don't under maintenance anymore.

henryonsoftware avatar Apr 17 '19 01:04 henryonsoftware

I got this error when access to themes option: Undefined property: stdClass::$id (View: /home/vagrant/code/test/vendor/tcg/voyager/resources/views/formfields/image.blade.php)

After 1 hour deep dive into Voyager Admin source I found the problem is Voyager Admin updated file image.blade.php at line 5, they want get $dataTypeContent->id But helpers.php missing it at line 17 $dataTypeContent = (object)[$key => $content];.

I tried change it to $dataTypeContent = (object)["id" => 0, $key => $content]; and it worked.

I don't know how to fix it, I just report issue and hope you guys fix it as correct way. Thanks!

Your fix worked for me for my Laravel Wave deployment, thank you very much!

rbrunner avatar Apr 24 '19 15:04 rbrunner

@rbrunner you can safe-fix follow @Padrio suggestion while waiting for my PR https://github.com/thedevdojo/voyager-themes/pull/25 be approved.

henryonsoftware avatar Apr 25 '19 08:04 henryonsoftware