Model accessors not working when response is JSON and field contains a number












2















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);
}


Name of fields in table:




  • address_line_1

  • address_line_2


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 and returns the field untouched.



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?










share|improve this question

























  • Does the date_of_birth mutators work correctly in the Json response? that's very strange

    – gbalduzzi
    Nov 23 '18 at 10:42













  • @gbalduzzi Yes it does, it's very odd, I have around 15 other fields which work fine but it's just these 2 which don't work. The only difference with these 2 is the fact that their field names contain a number.

    – Graeme
    Nov 23 '18 at 10:45













  • Can you check if the output of $user->toJson() is correct?

    – gbalduzzi
    Nov 23 '18 at 10:47











  • You can also try return response()->json($user->toArray())

    – gbalduzzi
    Nov 23 '18 at 10:53











  • @gbalduzzi that made no difference unfortunately. Just checking the output...

    – Graeme
    Nov 23 '18 at 11:01


















2















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);
}


Name of fields in table:




  • address_line_1

  • address_line_2


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 and returns the field untouched.



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?










share|improve this question

























  • Does the date_of_birth mutators work correctly in the Json response? that's very strange

    – gbalduzzi
    Nov 23 '18 at 10:42













  • @gbalduzzi Yes it does, it's very odd, I have around 15 other fields which work fine but it's just these 2 which don't work. The only difference with these 2 is the fact that their field names contain a number.

    – Graeme
    Nov 23 '18 at 10:45













  • Can you check if the output of $user->toJson() is correct?

    – gbalduzzi
    Nov 23 '18 at 10:47











  • You can also try return response()->json($user->toArray())

    – gbalduzzi
    Nov 23 '18 at 10:53











  • @gbalduzzi that made no difference unfortunately. Just checking the output...

    – Graeme
    Nov 23 '18 at 11:01
















2












2








2








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);
}


Name of fields in table:




  • address_line_1

  • address_line_2


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 and returns the field untouched.



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?










share|improve this question
















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);
}


Name of fields in table:




  • address_line_1

  • address_line_2


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 and returns the field untouched.



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?







php laravel laravel-5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 12:12









gbalduzzi

1,389622




1,389622










asked Nov 23 '18 at 10:31









GraemeGraeme

1941110




1941110













  • Does the date_of_birth mutators work correctly in the Json response? that's very strange

    – gbalduzzi
    Nov 23 '18 at 10:42













  • @gbalduzzi Yes it does, it's very odd, I have around 15 other fields which work fine but it's just these 2 which don't work. The only difference with these 2 is the fact that their field names contain a number.

    – Graeme
    Nov 23 '18 at 10:45













  • Can you check if the output of $user->toJson() is correct?

    – gbalduzzi
    Nov 23 '18 at 10:47











  • You can also try return response()->json($user->toArray())

    – gbalduzzi
    Nov 23 '18 at 10:53











  • @gbalduzzi that made no difference unfortunately. Just checking the output...

    – Graeme
    Nov 23 '18 at 11:01





















  • Does the date_of_birth mutators work correctly in the Json response? that's very strange

    – gbalduzzi
    Nov 23 '18 at 10:42













  • @gbalduzzi Yes it does, it's very odd, I have around 15 other fields which work fine but it's just these 2 which don't work. The only difference with these 2 is the fact that their field names contain a number.

    – Graeme
    Nov 23 '18 at 10:45













  • Can you check if the output of $user->toJson() is correct?

    – gbalduzzi
    Nov 23 '18 at 10:47











  • You can also try return response()->json($user->toArray())

    – gbalduzzi
    Nov 23 '18 at 10:53











  • @gbalduzzi that made no difference unfortunately. Just checking the output...

    – Graeme
    Nov 23 '18 at 11:01



















Does the date_of_birth mutators work correctly in the Json response? that's very strange

– gbalduzzi
Nov 23 '18 at 10:42







Does the date_of_birth mutators work correctly in the Json response? that's very strange

– gbalduzzi
Nov 23 '18 at 10:42















@gbalduzzi Yes it does, it's very odd, I have around 15 other fields which work fine but it's just these 2 which don't work. The only difference with these 2 is the fact that their field names contain a number.

– Graeme
Nov 23 '18 at 10:45







@gbalduzzi Yes it does, it's very odd, I have around 15 other fields which work fine but it's just these 2 which don't work. The only difference with these 2 is the fact that their field names contain a number.

– Graeme
Nov 23 '18 at 10:45















Can you check if the output of $user->toJson() is correct?

– gbalduzzi
Nov 23 '18 at 10:47





Can you check if the output of $user->toJson() is correct?

– gbalduzzi
Nov 23 '18 at 10:47













You can also try return response()->json($user->toArray())

– gbalduzzi
Nov 23 '18 at 10:53





You can also try return response()->json($user->toArray())

– gbalduzzi
Nov 23 '18 at 10:53













@gbalduzzi that made no difference unfortunately. Just checking the output...

– Graeme
Nov 23 '18 at 11:01







@gbalduzzi that made no difference unfortunately. Just checking the output...

– Graeme
Nov 23 '18 at 11:01














1 Answer
1






active

oldest

votes


















2














I can't verify this now, but i think it may be related to the way laravel computes snake_case and camel_case.



Basically, when you request a field like $user->address_line_1, laravel transforms the field in camelCase (AddressLine1) and checks for a custom accessors, it finds it and returns the correct modified value.



However, when the model is serialized into Json, it does the opposing operation: it tries to detect which field needs to be modified by looking at the accessors. So, it finds the getAddressLine1Attribute and transforms it into snake_case.. yielding address_line1, a wrong field.



The basic problem here is that both address_line_1 and address_line1 have the same camelCase representation, so it's impossible to reliably reverse the transformation to camelCase.



An hack you could try is to define the accessor as getAddressLine_1Attribute, but it won't work when accessing the field directly ($user->address_line_1)



You could define it to use the previously defined accessors, so you don't have code repetition:



public function getAddressLine_1Attribute($value)
{
return $this->getAddressLine1Attribute($value);
}

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


EDIT: my theory has been confirmed by some testing with the camel_case() and snake_case() helper functions






share|improve this answer


























  • Thanks, that worked perfectly. I have also kept my other accessors so that I can still use it both in the blade as well as with JSON.

    – Graeme
    Nov 23 '18 at 11:11













  • Great! I edited the post with a quick way to avoid code repetiton, glad i helped :)

    – gbalduzzi
    Nov 23 '18 at 11:18











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444989%2fmodel-accessors-not-working-when-response-is-json-and-field-contains-a-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














I can't verify this now, but i think it may be related to the way laravel computes snake_case and camel_case.



Basically, when you request a field like $user->address_line_1, laravel transforms the field in camelCase (AddressLine1) and checks for a custom accessors, it finds it and returns the correct modified value.



However, when the model is serialized into Json, it does the opposing operation: it tries to detect which field needs to be modified by looking at the accessors. So, it finds the getAddressLine1Attribute and transforms it into snake_case.. yielding address_line1, a wrong field.



The basic problem here is that both address_line_1 and address_line1 have the same camelCase representation, so it's impossible to reliably reverse the transformation to camelCase.



An hack you could try is to define the accessor as getAddressLine_1Attribute, but it won't work when accessing the field directly ($user->address_line_1)



You could define it to use the previously defined accessors, so you don't have code repetition:



public function getAddressLine_1Attribute($value)
{
return $this->getAddressLine1Attribute($value);
}

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


EDIT: my theory has been confirmed by some testing with the camel_case() and snake_case() helper functions






share|improve this answer


























  • Thanks, that worked perfectly. I have also kept my other accessors so that I can still use it both in the blade as well as with JSON.

    – Graeme
    Nov 23 '18 at 11:11













  • Great! I edited the post with a quick way to avoid code repetiton, glad i helped :)

    – gbalduzzi
    Nov 23 '18 at 11:18
















2














I can't verify this now, but i think it may be related to the way laravel computes snake_case and camel_case.



Basically, when you request a field like $user->address_line_1, laravel transforms the field in camelCase (AddressLine1) and checks for a custom accessors, it finds it and returns the correct modified value.



However, when the model is serialized into Json, it does the opposing operation: it tries to detect which field needs to be modified by looking at the accessors. So, it finds the getAddressLine1Attribute and transforms it into snake_case.. yielding address_line1, a wrong field.



The basic problem here is that both address_line_1 and address_line1 have the same camelCase representation, so it's impossible to reliably reverse the transformation to camelCase.



An hack you could try is to define the accessor as getAddressLine_1Attribute, but it won't work when accessing the field directly ($user->address_line_1)



You could define it to use the previously defined accessors, so you don't have code repetition:



public function getAddressLine_1Attribute($value)
{
return $this->getAddressLine1Attribute($value);
}

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


EDIT: my theory has been confirmed by some testing with the camel_case() and snake_case() helper functions






share|improve this answer


























  • Thanks, that worked perfectly. I have also kept my other accessors so that I can still use it both in the blade as well as with JSON.

    – Graeme
    Nov 23 '18 at 11:11













  • Great! I edited the post with a quick way to avoid code repetiton, glad i helped :)

    – gbalduzzi
    Nov 23 '18 at 11:18














2












2








2







I can't verify this now, but i think it may be related to the way laravel computes snake_case and camel_case.



Basically, when you request a field like $user->address_line_1, laravel transforms the field in camelCase (AddressLine1) and checks for a custom accessors, it finds it and returns the correct modified value.



However, when the model is serialized into Json, it does the opposing operation: it tries to detect which field needs to be modified by looking at the accessors. So, it finds the getAddressLine1Attribute and transforms it into snake_case.. yielding address_line1, a wrong field.



The basic problem here is that both address_line_1 and address_line1 have the same camelCase representation, so it's impossible to reliably reverse the transformation to camelCase.



An hack you could try is to define the accessor as getAddressLine_1Attribute, but it won't work when accessing the field directly ($user->address_line_1)



You could define it to use the previously defined accessors, so you don't have code repetition:



public function getAddressLine_1Attribute($value)
{
return $this->getAddressLine1Attribute($value);
}

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


EDIT: my theory has been confirmed by some testing with the camel_case() and snake_case() helper functions






share|improve this answer















I can't verify this now, but i think it may be related to the way laravel computes snake_case and camel_case.



Basically, when you request a field like $user->address_line_1, laravel transforms the field in camelCase (AddressLine1) and checks for a custom accessors, it finds it and returns the correct modified value.



However, when the model is serialized into Json, it does the opposing operation: it tries to detect which field needs to be modified by looking at the accessors. So, it finds the getAddressLine1Attribute and transforms it into snake_case.. yielding address_line1, a wrong field.



The basic problem here is that both address_line_1 and address_line1 have the same camelCase representation, so it's impossible to reliably reverse the transformation to camelCase.



An hack you could try is to define the accessor as getAddressLine_1Attribute, but it won't work when accessing the field directly ($user->address_line_1)



You could define it to use the previously defined accessors, so you don't have code repetition:



public function getAddressLine_1Attribute($value)
{
return $this->getAddressLine1Attribute($value);
}

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


EDIT: my theory has been confirmed by some testing with the camel_case() and snake_case() helper functions







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 11:15

























answered Nov 23 '18 at 11:05









gbalduzzigbalduzzi

1,389622




1,389622













  • Thanks, that worked perfectly. I have also kept my other accessors so that I can still use it both in the blade as well as with JSON.

    – Graeme
    Nov 23 '18 at 11:11













  • Great! I edited the post with a quick way to avoid code repetiton, glad i helped :)

    – gbalduzzi
    Nov 23 '18 at 11:18



















  • Thanks, that worked perfectly. I have also kept my other accessors so that I can still use it both in the blade as well as with JSON.

    – Graeme
    Nov 23 '18 at 11:11













  • Great! I edited the post with a quick way to avoid code repetiton, glad i helped :)

    – gbalduzzi
    Nov 23 '18 at 11:18

















Thanks, that worked perfectly. I have also kept my other accessors so that I can still use it both in the blade as well as with JSON.

– Graeme
Nov 23 '18 at 11:11







Thanks, that worked perfectly. I have also kept my other accessors so that I can still use it both in the blade as well as with JSON.

– Graeme
Nov 23 '18 at 11:11















Great! I edited the post with a quick way to avoid code repetiton, glad i helped :)

– gbalduzzi
Nov 23 '18 at 11:18





Great! I edited the post with a quick way to avoid code repetiton, glad i helped :)

– gbalduzzi
Nov 23 '18 at 11:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53444989%2fmodel-accessors-not-working-when-response-is-json-and-field-contains-a-number%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'