Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Database, Table and Column Naming Conventions?

I'm using laravel eloquent data objects to access my data, what is the best way to name my tables, columns, foreign/primary keys etc?

I found, there are lots of naming conventions out there. I'm just wondering which one best suits for laravel eloquent models.

I'm thinking of following naming convention:

  1. Singular table names (ex: Post)
  2. Singular column names (ex: userId - user id in the post table)
  3. Camel casing for multiple words in table names (ex: PostComment, PostReview, PostPhoto)
  4. Camel casing for multiple words in column names (ex: firstName, postCategoryId, postPhotoId)

So with this, I could use similar syntax in the controller.

$result = Post::where('postCategoryId', '4')->get(); 

Are there any recommended Laravel guidelines for this? Can I proceed with these naming conventions?

If someone has better suggestions, I will be very happy to hear them.Thanks a lot!

like image 260
stackflow Avatar asked Sep 19 '14 06:09

stackflow


People also ask

How do you name columns in a table?

Each table name and column name should start with a capital letter. The table name should be end with letter "s" (or "es") to indicate plural. If the index name contains more than one word then the first character of each word should be a capital and separated with an underscore (_).

What is the right naming convention for table?

When naming tables, you have two options – to use the singular for the table name or to use a plural. My suggestion would be to always go with names in the singular. If you're naming entities that represent real-world facts, you should use nouns. These are tables like employee, customer, city, and country.

What is naming convention in laravel?

Naming Models in LaravelA model should be in singular, no spacing between words, and capitalised. For example: `User` (`\App\User` or `\App\Models\User`, etc), `ForumThread`, `Comment`. Bad examples: Users , ForumPosts , blogpost , blog_post , Blog_posts . But of course you can just set $this->table in your model.

What are the naming conventions for database design?

Database names must always start with a letter. Database names starting with an underscore are considered to be system databases, and users should not create or delete those. The maximum allowed length of a database name is 64 bytes. Database names are case-sensitive.


1 Answers

Laravel has its own naming convention. For example, if your model name is User.php then Laravel expects class 'User' to be inside that file. It also expects users table for User model. However, you can override this convention by defining a table property on your model like,

    class User extends Eloquent implements UserInterface, RemindableInterface {         protected $table = 'user';     } 

From Laravel official documentation:

Note that we did not tell Eloquent which table to use for our User model. The lower-case, plural name of the class will be used as the table name unless another name is explicitly specified. So, in this case, Eloquent will assume the User model stores records in the users table. You may specify a custom table by defining a $table property on your model

If you will use user table id in another table as a foreign key then, it should be snake-case like user_id so that it can be used automatically in case of relation. Again, you can override this convention by specifying additional arguments in relationship function. For example,

    class User extends Eloquent implements UserInterface, RemindableInterface {         public function post(){             return $this->hasMany('Post', 'userId', 'id');         }     }      class Post extends Eloquent{         public function user(){             return $this->belongsTo('User', 'userId', 'id');         }        } 

Docs for Laravel eloquent relationship

For other columns in table, you can name them as you like.

I suggest you to go through documentation once.

like image 68
user4055288 Avatar answered Sep 24 '22 21:09

user4055288