vendredi 23 novembre 2018

Model "get" mutator not working when response is JSON

I have many "get" mutators which decrypt data so that it can be readable in the view. These mutators are the names of the fields in the DB. Most of these work except for 2. The only difference with these 2 mutators is that the mutator function names contains numbers.

Example of a mutator which works:

public function getDateOfBirthAttribute($value)
{
    return empty($value) ? '' : decrypt($value);
}

Examples of mutators which do NOT work:

public function getAddressLine1Attribute($value)
{
    return empty($value) ? '' : decrypt($value);
}

public function getAddressLine2Attribute($value)
{
    return empty($value) ? '' : decrypt($value);
}

The very strange thing about all this is when the response is NOT json, the mutator works as expected and decrypts the field. (e.g. using return view('view.name', compact($user))), however when I put this data into a JSON response (e.g. return response()->json([$user]);, the 2 address line mutators don't work.

I have tried adding return "test" to these 2 mutators to see if it is even hitting the function but it is not.

Why in this instance does JSON stop the mutator from working? Could it be an issue with numbers in the function name? May I have to rename my fields in the DB?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire