Replies: 2 comments 2 replies
-
Nice catch |
Beta Was this translation helpful? Give feedback.
-
Why was this changed to a discussion? This seems to be a legitimate issue with Eloquent when it comes to The documentation for mutators/accessors state that you should use the Defining An Accessor In our case the columns is Once the code gets to this point in the execution it says there is no such column Wouldn't it just make more sense in the following method to first check for the snake case attribute and then if not found check for the camel case attribute? I am honestly not sure why this is such a large issue either, when I search the Illuminate folder for the string It seems like a check could be made on the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
10.8
PHP Version
8.2.10
Database Driver & Version
MySQL on AWS
Description
I am trying to use Laravel 10.8 mutators using the Attribute return type. The issue I am having is that some of our database columns are
snake_case
and some arecamelCase
. Having both column types is something that we currently have to live with as modifying the table with millions of records is not an option at this time. What I am finding is that when I call$model->toArray()
the mutators are being executed for oursnake_case
columns but not for ourcamelCase
columns.It appears to be failing in the
HasAttributes
trait on the following lines. It seems to think thecamelCase
attribute issnake_case
and since it does not find the snake case attribute itcontinue
s.I have found that for some reason in the HasAttributes.php file our column of
camelCaseName
is listed in thestatic::$snakeAttributes
array on the model assnake_case_name
, therefore it thinks there is a columnsnake_case_name
on our model so I assume that the mutator is trying to mutate that attribute which does not exist. Currently all of ourcamelCase
attributes are ignored when the mutator is executed when the model is converted to an Array.The
camelCase
mutators only seem to work if we manually call$model->camelCaseName
. In this case the mutator is executed. I have tried setting the models$snakeAttributes
property tofalse
as it is true by default, when I do this it causes issues with our mutators forsnake_case
columns. Is there a way to make Laravel support both snake and camel case attributes when using mutators?I have found a work-around but it is tedious and requires all developers to
remember
they have to do this, we would have to set$snakeAttributes = false
which will allow thesnakeCase
column and mutator to run, however if we then add mutators for oursnake_case
columns we have to define two methods for it to work. If we define only thesnake_case
mutator then an error is thrown that it cannot find thecamelCaseName()
method. If we don't define thecamelCaseName
mutator method then the mutation is not completed on thesnake_case
attribute.In order to make this work for both camelCase and snake_case columns on a model where both will have Attribute mutators you need to set
$snakeAttributes = false
and then for allsnake_case
columns that have mutators you will need to do the followingI have been trying to trace the issue through HasAttributes but was unable to determine why this is happening and ran out of time at the moment.
Steps To Reproduce
Create a database with a table having both snake_case and camelCase columns of type Decimal.
Create a model for the table using the code in the description naming your mutators to match your column names.
Add the following model property
public static $snakeAttributes = false;
Start Tinker
Find a record and assign to a variable
$variable = MyModel::find(1);
Call
$variable->toArray()
and notice the outcome is as explained in the description.Remove the
snake_case
mutator from your modelRestart Tinker for code changes.
Find a record and assign to a varable
$variable = MyModel::find(1);
Call
$variable->toArray()
notice there are no errors but thesnake_case
mutator did not mutate the data.Remove the
camelCase
mutator for thesnake_case
column from your modelRestart Tinker for code changes.
Find a Find a record and assign to a varable
$variable = MyModel::find(1);
Call
$variable->toArray()
notice the erorr thatcamelCaseName
method was not defined.Remove the
$snakeAttributes = false
from the model and thenRestart Tinker for code changes.
Find a record and assign to a variabel
$variable = MyModel::find(1);
call
$variable->toArray()
and notice thecamelCase
column value is not mutated but thesnake_case
value was.Beta Was this translation helpful? Give feedback.
All reactions