Can I use camelCase with Laravel 4 eloquent?

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

For example: when I try to use "jobRole", I get "job_role". I want this to be a "jobRole".

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

+6
source share
3 answers

Found that you can do this.

In the model, override "snakeAttributes" to "false" as follows:

class YourModel extends Eloquent{ public static $snakeAttributes = false; } 
+18
source

Now you can. I just completed a package that allows you to fully work with model attributes in camelCase, if necessary.

https://github.com/kirkbushell/eloquence

Install the package as usual:

 composer require kirkbushell/eloquence ~1.0 

Then configure your service providers:

 'KirkBushell\Eloquence\EloquenceServiceProvider', 

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

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

This ensures that the library is basically a replacement. If you just want to test some models, you can simply extend the class of the base Eloquence model:

 class MyModel extends \KirkBushell\Eloquence\Database\Model 

I built this class mainly because it hurt me to see snake_case in our javascript applications, but also in our server code. This ensured that everywhere (front-end and back-end) deals with the camel body.

+1
source

I can’t be afraid. This error: https://github.com/laravel/laravel/issues/788 explains more, but long and short is that you need field names that must be lowercase for models to work.

0
source

Source: https://habr.com/ru/post/953026/


All Articles