Laravel 5 Naming Conventions [closed]
Asked Answered
C

3

23

I am bit confused with Laravel conventions as I am new to this framework. I am following Jeffrey Way Laracasts videos he uses Plural for Controller names.

E.g.: PagesController, Cards Controller, PostsController

But if I refer official documentations of Laravel > Controllers and Laravel > Tutorials > Quick Start > Intermediate Task List it uses Singular names.

E.g.: PhotoController, TaskController

Can anybody please list down the official coding conventions for following entities?

Tables: posts, comments, post_comments or Post, Comment, PostComment

Columns: id, post_id, comment_id or id, postId, commentId

Controllers: PagesController, Cards Controller, PostsController or PhotoController, TaskController

Models: Pages, Cards, Posts or Page, Card, Post

Croaky answered 14/1, 2017 at 22:12 Comment(2)
I would agree with both below apart from naming controllers. I've seen people use both singular and plural for controllers and quite honestly it's whichever makes more sense to you. Just be consistent with whichever one you go with.Brandibrandice
Voted to close this as opinion-based; there are some naming conventions that Laravel expects in its default configuration, but these are all changeable in the app code, and in any case controller names are not subject to these expectations.Laudatory
M
46

Tables: posts, comments, comment_post

Columns: id, post_id, comment_id

Controllers: PhotoController, TaskController

Models: Page, Card, Post

For more details check out my Laravel naming conventions table.

Metrics answered 14/1, 2017 at 22:22 Comment(7)
isn't it post_comments? I think not post_commentCroaky
No, it's actually should be comment_post which is derived from foreign key names in the alphabetical order.Metrics
It is confusing, if the database tables naming convention is snake case plurals why it is comment_post? It should be post_comments because 1 post can have many comments. Anyway according to documentation if I want I can use $table = 'post_comments'; code snippet to change default table name to my own one under model class.Croaky
@YohanHirimuthugoda you're talking about pivot table, usually, those do not have their own model. But you can define custom pivot table name when defining belongsToMany() relationship. I wouldn't recommend you to do that, though. It's always better to follow conventions to create readable and maintainable apps.Metrics
Thank you very much for your quick reply. I will follow conventions as much as I can, as suggested by you. Another small thing I need to clarify; When I need to group particular controller's views, is it plural or singular folder names? E.g.: posts or postCroaky
What about model names that contain abbreviations. E.g ERC20.Squashy
Just to clarify if anyone is confused (as I was). When defining a model for a pivot table, the class Illuminate\Database\Eloquent\Relations\Pivot can be used, which fixes assuming plural table names for models.Torray
P
6

Remember that "conventions" are just conventions and you could do whatever you want just be constant, however it's better follow the documentation:

  1. Table name: plural and _ to separate words (users, tags, ...)
  2. Columns name: singular and _ to separate words (user, tag, ...)
  3. Models: singular with first letter capitalized and capitalization to separate words (User, Tag, ...)
  4. Controllers: singular with capitalized first letter and capitalization to separate words followed by "Controller" (UserController, TagController, ...)
Purr answered 14/1, 2017 at 22:21 Comment(4)
Controller name shouldn't rely on a model name. In a real app there are a lot of controllers that do not have "it's own model" and controllers that use many models.Metrics
Yes, if a controller is associate with a model it's better associate them but if you can't just singular + "Controller" (TagController, UserController, ...)Purr
This is how to name a model with a name consists of multiple words: UserMetaKey. A model with that name expects a table with the name user_meta_keys to deal with.Dashpot
what about in the blade file, name="modelname"Feathery
B
2

I know I'm from old school (coding since 1984 on computers, though having evolved with PHP and js/ECMAScript since their first version), but the good old Merise rule "never use plural" had strong and good resasons to be respected. What a pity Eloquent forces us to use table name in plural.

PSR-2 or PSR-1 doesn't indicate anything about plural or singular, but for simplicity sake, I heavily recommend to always use singular, excepte where the "system" needs it (as Eloquent does). In that case, don't mix plural and singular. On the datatables used by laravel, we use table names in plural. That's a singularity (compared to the other developments already made or with which we communicate), but all the tables are in plural on that part.

And NEVER make a typo when naming something (example: 'personnal' i.o. 'personal', etc.). Doublecheck first. Orthograph rules.

Brost answered 11/1, 2018 at 12:24 Comment(2)
I wholeheartedly agree with this. I NEVER want to have to remember various rules for what is pluralized and what is not. EVERYTHING is singular, no exceptions.Rhodian
I agree too. It would be great if you could define your preferences for this type of thing in a config file that Laravel reads. Then you could certainly base your names on their recommendations, but be free to override when things like Eloquent break the paradigm.Sloan

© 2022 - 2024 — McMap. All rights reserved.