Search code examples
phplaravelmodelpackagevendor

How can I access a protected property of a Model located in a package (vendor folders)


The model is in the vendor folder (normal package). I need to access an attribute of the model which is protected. NOTE: Not possible of making a getter in the model class.


Solution

  • To access a class property that is marked as protected you need to extend that class. Note that if that class is marked as final, you won't be able to do it.

    Example:

    class Parent {
    
        protected $property;
    
    }
    
    class Child extends Parent {
    
        public function getProperty()
        {
            return $this->property;
        }
    
    }