- Laravel -

Naming Conventions in Laravel

|
3 min read
Naming Conventions in Laravel

Laravel is a wonderful PHP framework which is used by many developers to build different types of web applications and APIs. It has a lot of conventions and best practices that are followed by the community and the framework itself. In this blog post, I'll be discussing some of the naming conventions in Laravel that I personally follow.

Controllers

Naming: Use PascalCase (CapitalCase) for controller names. They should be singular, and should end with the word "Controller". Example:

  • Good: ArticleController, UserController, CommentController
  • Bad: ArticlesController, UsersController, Comments

Methods in Controllers

Naming: Use camelCase (first character lowercase) for method names in controllers. In Laravel, the method names are usually in the format of actionName. Example:

VERBURLController MethodRoute Name
GET/articlesindexarticles.index
POST/articles/createcreatearticles.create
POST/articlesstorearticles.store
GET/articles/{article}showarticles.show
GET/articles/{article}/editeditarticles.edit
PUT/articles/{article}updatearticles.update
DELETE/articles/{article}destroyarticles.destroy

Database Tables

Naming: Use lowercase with underscores to separate words (snake_case) and always use plural names. Example:

  • Good: users, articles, comments
  • Bad: all_users, Articles, tbl_comments, blogPosts

Database Columns

Naming: Use snake_case, in lowercase, and do not reference the table name. Example:

  • Good: first_name, last_name, email
  • Bad: FirstName, LastName, UserEmail, tbl_user_email

Pivot Tables

Naming: Use lowercase, place each model name in alphabetical order, and use underscores to separate words (snake_case). Example:

  • Good: post_user, category_product, product_variant
  • Bad: posts_users, product_category, productVariants

Primary Keys

Naming: Always use id as the primary key name.

Foreign Keys

Naming: Always use the name of the table that the foreign key references, followed by _id. Example:

  • Good: user_id, article_id, comment_id
  • Bad: UserId, ArticleId, CommentId

Migration Files

Naming: Use proper names for migration files like create_users_table.php, create_articles_table.php, add_status_column_to_users_table.php, alter_users_table.php, etc prefixed with timestamps.

Foreign Keys

Naming: Always use the name of the table that the foreign key references, followed by _id.

Variables

Naming: Use camelCase (first character lowercase) for variable names. Use plural names for variables that represent multiple records. Use descriptive names for variables not $data, $d, etc. Example:

  • Good: $user, $users, $article, $articles
  • Bad: $Users, $data

Models

Naming: Use PascalCase (CapitalCase), in singular form. Example:

  • Good: User, Article, Comment
  • Bad: Users, Articles, comments, user_articles, Article_Comments

Model Attributes

Naming: Use snake_case, in lowercase. Follow the same convention as database column names. Example:

  • Good: first_name, last_name, email
  • Bad: FirstName, LastName, UserEmail, tbl_user_email

Model Methods

Naming: Use camelCase (first character lowercase) for method names in models. Use plural names for methods that represent multiple records. Example:

  • Good: getUsers, getArticles, getComments
  • Bad: GetUsers, GetAllData, get_comments

Model Relationships

Naming: Use camelCase (first character lowercase) for relationship names.

HasOne or BelongsTo (one-to-one)

Naming: Use singular names, following camelCase convention. Example:

  • Good: public function user(): BelongsTo, public function category(): HasOne
HasMany, BelongsToMany, or HasManyThrough (one-to-many)

Naming: Use plural names, following camelCase convention. Example:

  • Good: public function users(): HasMany, public function articles(): BelongsToMany
Polymorphic Relationships

Naming: Use appropriate names like media, and Laravel will automatically handle the column names for you like mediable_id and mediable_type.

Traits

Naming: Use PascalCase (CapitalCase), adjective-based if possible. Example:

  • Good: Dispatchable, Notifiable, Sortable, ImplementsPagination, HasRoles

Blade Views

Naming: Use lowercase, with underscores/kebab-case for view names. I prefer to use kebab-case. Example:

  • Good: index.blade.php, show.blade.php, create.blade.php, product-comments.blade.php

Routes:

Naming: Use lowercase, plural names for routes. Example:

  • Good: /articles/slug, /users/profile, profiles/:id, /product-comments
  • Bad: /article/slug, /user_profile/, profile/:id, /product_comments

Named Routes:

Naming: Use snake_case or kebab-case for named routes separated by dots. Example:

  • Good: route('articles.index'), route('users.profile'), route('profiles.show', ['id' => 1]), route('product-comments.index')
  • Bad: route('article.index'), route('user_profile'), route('profile_show', ['id' => 1])

Config and Language Files:

Naming: Use snake_case, in lowercase, for config and language files.

Conclusion

These are some of the naming conventions that I follow in Laravel. I hope they help you to write better code and make your code more readable. This is just a small subset of the conventions, and there are many more. If you have any suggestions or feedback, please let me know.

Keep coding, keep exploring, and keep inspiring. 🐼

Written By Anuz Pandey

Anuz Pandey is a multi-disciplinary designer and developer with a passion for creating visually stunning and user-friendly digital experiences. He has been working in the industry for over 5 years. He is currently working as a freelance designer and developer and is available for hire.

Tags

  • Laravel
  • Naming Conventions
  • PHP