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:
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!
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 (_).
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With