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:

  • Singular table names (ex: Post)
  • Singular column names (ex: userId - user id in the post table)
  • Camel casing for multiple words in table names (ex: PostComment, PostReview, PostPhoto)
  • 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!


    Laravel has it's 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.


    I don't agree in general with these examples you both have shown right on here.

    It is clean if you take a look at the official Laravel documentation, especially in the Eloquent's relationship session (http://laravel.com/docs/4.2/eloquent#relationships).

    Table names should be in plural, ie 'users' table for User model.

    And column names don't need to be in Camel Case, but Snake Case. See it is already answered: Database/model field-name convention in Laravel?

    It is too usual you can see it is like the RedBeanORM: the Snake Case for columns, even if you try other one. And it is adviced to avoid repeating the table names with column ones due to the method you can call from the Model object to access their relationships.


    The default table naming conventions can easily cause conflicts with the installation of multiple packages who may have incidentally the same class names. A solution would be to name tables as: [vendor].[package].[class], which is in line with how namespacing in Laravel is applied.

    Edited : Using dots in table names is not recommended though. Would there be an alternative convention to use to ensure developers of a modular built application do not need to worry about existing table names.

    链接地址: http://www.djcxy.com/p/41314.html

    上一篇: 应用程序和数据库架构

    下一篇: 数据库,表和列命名约定?