Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use camelCase with Laravel 4 eloquent?

I have existing data in a mysql db whose columns are camelCased. I can't change them to snake case.

For instance: when I try to use "jobRole", I get "job_role". I want it to be "jobRole".

Is there a setting in laravel to support this? I've read about mutators but don't understand them. Is that the solution? If so, how do I do this?

like image 767
Chuck Avatar asked Sep 03 '13 04:09

Chuck


2 Answers

Discovered you can do this.

In the model, redefine "snakeAttributes" to "false" like this:

class YourModel extends Eloquent{
    public static $snakeAttributes = false;
}
like image 160
Chuck Avatar answered Nov 06 '22 17:11

Chuck


You can now. I've just completed a package that allows you to work with model attributes completely in camelCase, if required.

https://github.com/kirkbushell/eloquence

Install the package as per normal:

composer require kirkbushell/eloquence ~1.0

Then setup your service providers:

'KirkBushell\Eloquence\EloquenceServiceProvider',

Then all you need to do is either setup Eloquent to point to the new class as part of your aliases in application/config/app.php:

'Eloquent' => 'KirkBushell\Eloquence\Database\Model',

That ensures that the library is basically a drop-in replacement. If you want to just test with some models, you can simply extends the Eloquence base model class:

class MyModel extends \KirkBushell\Eloquence\Database\Model

I built this class mainly because I was sick of seeing snake_case in our javascript applications, but also in our server-side code. This ensured that everywhere (front-end and back-end) was dealing with camel casing.

like image 1
Oddman Avatar answered Nov 06 '22 17:11

Oddman