Database/model field-name convention in Laravel?
Asked Answered
H

1

6

I understand that camel case is the usual convention, as per http://forums.laravel.io/viewtopic.php?id=8857

However, Laravel is using created_at and updated_at for timestamp fields.

I'm confused as what would be the best naming convention for field-names in the database? If camel cased, can I then switch created_at to createdAt, or should I use camel case for own fields and snake case for the timestamps?

It doesn't seem clear to me. Maybe Laravel wants me to use snake case all the way, like first_name in the database and $first_name in the PHP code, rather than firstName in the database, and $firstName in the code?

I hope someone can clarify...

Hagfish answered 28/1, 2014 at 8:46 Comment(0)
A
13

Snake case is the convention for database fields and model attributes.

You can see it in the getter and setter (http://laravel.com/docs/eloquent#accessors-and-mutators) or relationships sections (http://laravel.com/docs/eloquent#relationships) in the documentation.

Alesiaalessandra answered 28/1, 2014 at 8:46 Comment(1)
So the correct way according to your answer would be code like: $firstName = $model->first_name;Hagfish

© 2022 - 2024 — McMap. All rights reserved.