custom accessors only work when called directly.
ex: $model->zip normally returns a zip id from db (ex: 33)
custom accessor makes $model->zip return actual zip code (49505)
$model->toJson() returns value from db, and not custom accessor.
Seems to me like ->toJson() or ->toArray() should make use of the accessors without having to go through and manually re-build an array of the model to be returned
In this particular case you really should use a default Eloquent relationship.
In order to force your attribute to be returned in the array, add it as a key to the $attributes array.
class User extends Eloquent {
protected $attributes = array(
'ZipCode' => '',
);
public function getZipCode()
{
return ....
}
}
Note: this particular code sample is un tested but I've done this a few times. Should work. Relevant
the above code seems to make no difference when dumping the model ->toJson/Array
i can just build a custom json array to return for now
This is intentional and for performance reasons.
Most helpful comment
This is intentional and for performance reasons.