Saturday, April 15, 2017

Serialize dynamic properties

I have an eloquent accessor that returns an object.

This object's class implements the magic __get() to dynamically retrieve properties, like so:

<?php

public function __get($property)
{
    if (method_exists($this, $property)) {
        return call_user_func([$this, $property]);
    }
}

I want this object's dynamic properties to be available in the json representation of the model, but can't figure out how.

I'm already using $appends on the model, so when the model is serialized the object is available, but it's obviously empty.



via RegularEverydayNormalGuy

Advertisement