javascript - Laravel and variable naming conventions (snake case + camel case) - Stack Overflow

We are following the Laravel standard of naming model attributes as snake_case_variables. Although Lara

We are following the Laravel standard of naming model attributes as snake_case_variables. Although Laravel is just the API layer in our system that talks to a Javascript frontend and many other applications.

All the consumers of our API have a strong preference for camel cased variables (e.g. javascript / React).

We have found it difficult to change the core model attributes e.g. created_at, updated_at, confirmation_password, model relations etc into snake case.

we have toyed wth and implemented transform layers to change the "casing" ing in and going out, although this just add to maintenance and another thing for developers to remember...

How can we easily convert all model attributes, relations and general Laravel bindings into camel case?

We are following the Laravel standard of naming model attributes as snake_case_variables. Although Laravel is just the API layer in our system that talks to a Javascript frontend and many other applications.

All the consumers of our API have a strong preference for camel cased variables (e.g. javascript / React).

We have found it difficult to change the core model attributes e.g. created_at, updated_at, confirmation_password, model relations etc into snake case.

we have toyed wth and implemented transform layers to change the "casing" ing in and going out, although this just add to maintenance and another thing for developers to remember...

How can we easily convert all model attributes, relations and general Laravel bindings into camel case?

Share Improve this question edited Apr 8, 2016 at 6:53 larsAnders 3,8131 gold badge17 silver badges19 bronze badges asked Apr 8, 2016 at 6:43 AndrewMcLaganAndrewMcLagan 14k25 gold badges96 silver badges166 bronze badges 1
  • If you leave these attributes, as snake_case, will these consumers throw errors or just won't be able to read those attributes? In second case, there probably is a solution, in the first, there is no easy work around. – naneri Commented Apr 8, 2016 at 8:19
Add a ment  | 

2 Answers 2

Reset to default 3

I didn't remend as it changes the core of the laravel and so its modifying vendor files and it won't be easy to update without loosing the changes, but I think the easiest way is to do this is to replace the vendor\laravel\framework\src\illuminate\Support\Str.php with a modified version. laravel performs all string modifications to studly, camel case, snake case etc from methods inside this file. go through the functions change the way the functions perform, but i don't think it will make sense as the method names wouldn't be matching to what they are performing.

Best but the hard way is to go into each files that is being using the methods in Str class and modifying acording to your needs, than it will make sense and yet a lot of work as you need to change the methods that are being used.

change the required values from these files.

see vendor\laravel\framework\src\Database\Eloquent\Model.php also you could see these values are set here for checking from models.

public static $manyMethods = ['belongsToMany', 'morphToMany', 'morphedByMany'];
const CREATED_AT = 'created_at';
const UPDATED_AT = 'updated_at';

see vendor\laravel\framework\src\Database\Schema\Blueprint.php also you could see these values are set here for checking from creating migrations and dropping migrations. for example the one that creates timestamps.

public function timestamps()
{
    $this->timestamp('created_at')->nullable();

    $this->timestamp('updated_at')->nullable();
}

You should definitely take a look at Mappable. We had the same problem, and this solved it. You can map snake_case to CamelCase names and even use them in QueryBuilder.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248108a4618508.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信