Extending Exisitng Jsonable Property

A plugin have the following

public $jsonable = [
    'images', 
];

I would to add another field to this but failing. I tried

Gallery::extend(function($model) {
    $model->addDynamicProperty('jsonable', 'fields_object');
});

How can i achieve this?

Thank alot

Hi @ibrarartisan ,

I think it’s because it’s a protected variable.

Try this:

/**
     * boot method, called right before the request route.
     */
    public function boot()
    {

       
        Gallery::extend(function($model) {
             // Use reflection to access the protected jsonable property
            $reflector = new \ReflectionClass($model);
            $property = $reflector->getProperty('jsonable');
            
            // Make the property accessible
            $property->setAccessible(true);

            // Get the current value of the jsonable property
            $currentJsonable = $property->getValue($model);

            // Ensure 'fields_object' is added to the jsonable array
            if (!in_array('fields_object', $currentJsonable)) {
                $currentJsonable[] = 'fields_object';
            }

            // Set the modified value back to the jsonable property
            $property->setValue($model, $currentJsonable);
            
            // Optionally, set the property back to its original accessibility state
            $property->setAccessible(false);
        });
    }
1 Like

@apinard Thanks alot. works like a charm.

1 Like