---
title: "Naming"
description: "Naming classes, variables, database tables or anything else as a developer is always the hardest part."
canonical_url: "https://handbook.propay.co.za/php/naming"
---
# Naming

> Naming classes, variables, database tables or anything else as a developer is always the hardest part.

Naming a `class`, `model`, `controller`, `service`, `repository` should always be a `noun`. The key thing to understand is that a class represents a blueprint or template for the object.

## Controllers

Controllers should be in PascalCase/CapitalCase.

They should be singular, with no spacing between words, and end with "Controller".

Also, each word should be capitalized (i.e. `BlogController`, not `blogcontroller`).

<code-group>

```php [UserController.php]
class UserController
{
}
```

</code-group>

## Database Tables

### Tables

DB tables should be in lower case, with underscores to separate words (snake_case), and should be in plural form.

`posts`, `project_tasks`, `uploaded_images`

### Pivot Tables

Pivot tables should be all lower case, each model in alphabetical order, separated by an underscore (snake_case).

`post_user`, `task_user`

### Columns names

Table column names should be in snake_case (underscores between words), in lower case. You shouldn't reference the table name.

`post_body`, `id`, `created_at`

### Foreign Keys

Foreign keys should be the model name (singular), with `_id` appended to it (assuming the PK in the other table is `id`).

`comment_id`, `user_id`

## Variables

Normal variables should typically be in camelCase, with the first character lower case.

`$users = ...`, `$bannedUsers = ...`

If the variable contains an array or collection of multiple items then the variable name should be in plural. Otherwise, it should be in singular form.

For example: `$users = User::all();` (as this will be a collection of multiple User objects), but `$user = User::first()` (as this is just one object)

## Models

A model should be in PascalCase/CapitalCase. They should be in singular, no spacing between words, and capitalized.

`\App\Models\User`, `\App\Models\ForumThread`, `\App\Models\Comment`

### Properties

These should be lower case, snake_case. They should also follow the same conventions as the table column names.

`$this->updated_at`, `$this->title`

### Methods

Methods in your models in Laravel projects, like all methods in your Laravel projects, should be camelCase with the first character lower case.

`public function get()`, `public function getAll()`

### Relationships

**hasOne or belongsTo relationship (one to one / many to one)**

These should be singular form and follow the same naming conventions of normal model methods (camelCase, with the first letter lower case)

`public function postAuthor()`, `public function phone()`

**hasMany, belongsToMany, hasManyThrough (one to many)**

These follow the same naming conventions, however they should be plural.

`public function comments()`, `public function roles()`

**Polymorphic relationships**

These can be a bit awkward to get the naming correct.

Ideally, you want to be able to have a method such as this:

<code-group>

```php [PHP]
public function category()
{
    return $this->morphMany('App\Category', 'categoryable');
}
```

</code-group>

By default Laravel will assume there is a `categoryable_id` and a `categoryable_type` column.

But you can use the other optional parameters for `morphMany` ( `public function morphMany($related, $name, $type = null, $id = null, $localKey = null)`) to change the defaults.

## Controller Methods

These should follow the same rules as model methods, i.e. camelCase (first character lowercase).

In addition, for normal CRUD operations, they should use one of the following method names.

<table>
<thead>
  <tr>
    <th>
      Verb
    </th>
    
    <th>
      URI
    </th>
    
    <th>
      Action
    </th>
    
    <th>
      Route Name
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos
      </code>
    </td>
    
    <td>
      index
    </td>
    
    <td>
      photos.index
    </td>
  </tr>
  
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos/create
      </code>
    </td>
    
    <td>
      create
    </td>
    
    <td>
      photos.create
    </td>
  </tr>
  
  <tr>
    <td>
      POST
    </td>
    
    <td>
      <code>
        /photos
      </code>
    </td>
    
    <td>
      store
    </td>
    
    <td>
      photos.store
    </td>
  </tr>
  
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos/{photo}
      </code>
    </td>
    
    <td>
      show
    </td>
    
    <td>
      photos.show
    </td>
  </tr>
  
  <tr>
    <td>
      GET
    </td>
    
    <td>
      <code>
        /photos/{photo}/edit
      </code>
    </td>
    
    <td>
      edit
    </td>
    
    <td>
      photos.edit
    </td>
  </tr>
  
  <tr>
    <td>
      PUT/PATCH
    </td>
    
    <td>
      <code>
        /photos/{photo}
      </code>
    </td>
    
    <td>
      update
    </td>
    
    <td>
      photos.update
    </td>
  </tr>
  
  <tr>
    <td>
      DELETE
    </td>
    
    <td>
      <code>
        /photos/{photo}
      </code>
    </td>
    
    <td>
      destroy
    </td>
    
    <td>
      photos.destroy
    </td>
  </tr>
</tbody>
</table>

## Traits

Traits should be adjectives.

`Notifiable`, `Dispatchable`

## Blade

Blade files should be in lower case, snake_case (underscore between words).

`all.blade.php`, `all_posts.blade.php`


## Sitemap

See the full [sitemap](https://handbook.propay.co.za/sitemap.md) for all pages.
