JSON encoding all attribgutes for a Model

Hi everyone,

I am trying to JSON encode all attributes for a model, both from DB and not from DB. I am however experiencing difficulty even after doing what the documentation says should work.

So I using the code much like:


And in the documentation is states that the default param to get all attributes is true. Thing is I have two predefined (in the model) variables:

  • public $title_type

  • public $deleted

And these do not seem to be returned with the DB attributes of the model. So what I actually get back is:


Am I missing something to be able to return these two non-db varialbes from the model as well?

I mean, just in case I even added these to the attributeLabels() function in case that had something to do with their return but still nothing.


Unfortunately I eventually fixed this by making an actual __toJSON() function forcing the public attributes to be in there. Ugly as hell which I don’t like :(.

But thanks anyway,

In the end we solved this by using this piece of code when trying to get all the attributes including the non db connected attributes.

$safeAttributes = $model->getAttributes($model->safeAttributeNames);

By doing so it returns all the safe attributes including the non db attributes. Note that ithis will only be the case if there are validation rules for the attributes or they are set as safe.