Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Notdavis's avatar

camelCase variables and snake_case database columns

Laravel suggests camelCase for all variables inside and outside of classes/methods/functions. Laravel suggests snake_case for all columns in the database.

When it comes time to $Model->fill() | Model::create(), I find instead I'm writing lines and lines of:

$Model->snake_case1 = $data['camelCase1'];
$Model->snake_case2 = $data['camelCase2']; 
$Model->snake_case2 = $data['camelCase3']; 
$Model->snake_case3 = $data['camelCase4']; 

I can't decide if this is bad or good.

Some cursory searching didn't reveal any good answers, some talk about using the Str::snake() helper but I'm not that excited about creating something that iterates over arrays and ${Str::snake($key)} = $value.

Alternatively break standards and snake_case it all. What is the community's thought/solutions on tackling this minor problem?

0 likes
9 replies
tykus's avatar

The variable in this case is $data; the key is camelCase1 etc. And you're not using fill or create whenever you assign to the individual properties like that. If you're that worried about case, why is it $Model and not $model?

Personally, my array would look like:

$data = [
    'snake_case1' => 'value',
    // ...
];

Model::create($data);
Notdavis's avatar

@tykus

If you're that worried about case, why is it $Model and not $model?

This was a typo on my part.

I'm not sure I understand what you're explaining with the example $data array. In my case, $data is returned by a service, but yes, I could go in and modify the service to return $data with key names that are snake_case if that is what you mean.

I was in the camelCase mindset and was writing $data['camelCase'] = $camelCase; in the service without thinking about it.

Snapey's avatar

I would use

camelCase for all variables inside and outside of classes/methods/functions

but not for array keys

Similarly, all my form fields are snake (eg first_name) because they are not PHP variables,

Tray2's avatar

Variables and method names should be descriptive and use camel case if more than one word.

Table and column names should be snake case if more than one word

rajeshtva's avatar

The idea is as per my understanding is that:

in database column or table names are case-insensitive. hence having camel-case/all uppercase/all lowercase words won't matter. all are considered same thing. so to differentiate between user provided values (columns names & table names) & (database related keywords). the convention of snake case was adopted to make things look simpler.

on the other hand, in PHP variable names are case-sensitive, which means camelcase & other things matter. hence the suggestion.

edit: I meant all strings(any column names, table names, or keywords) are case-insensitive in most database.

Tray2's avatar

@rajeshtva The database isn't always case sensitive when it comes to column names, at least not the Oracle database, it all depends on how you define the column name. If I remember correctly it goes like this.

  • Column_Name (case insensitive)
  • 'Column_Name' (case insensitive)
  • "Column_Name" (Case sensitive)

I suggest that you should always use lowercase and snake case when defining columns.

rajeshtva's avatar

@Tray2

This is what i meant. all are case-insensitive. i have highlighted in in case-insensitive.

I myself use mysql (oracle). so i can confirm what you suggested is right.

Tray2's avatar

@rajeshtva A MySQL rdbms and an Oracle RDBMS are two different things, but I guess you know that, but just wanting to make it clear for any future visitors.

valentin_vranic's avatar

I know I't probably not the best approach (or at least it could be written more eloquent, or less robust), but in my case, using Livewire 3 Form (but this doesn't effect the scenario a lot), I have this solution. This in my case, collects all the fillables from a model, and returns all it's representatives from a form it it's present

$formData = $this->all();
$fillableProperties = Helper::intersectKeysWithCamelCaseAccess(
      $formData,
      array_flip($this->{$modelName}->getFillable())
);
public static function intersectKeysWithCamelCaseAccess(array $formData, array $fillableAttributes): array
{
        $filteredData = [];

        foreach (array_flip($fillableAttributes) as $fillableAttribute) {
            $camelCaseAttribute = Str::camel($fillableAttribute);
            if (isset($formData[$camelCaseAttribute])) {
                $filteredData[$fillableAttribute] = $formData[$camelCaseAttribute];
            }
        }

        return $filteredData;
}

Please or to participate in this conversation.