# Laravel Naming Conventions Guide

Laravel is often referred to as a **magical framework**, and for good reason. When you follow its default naming conventions, it feels like Laravel can read your mind. Things work without you having to write extra code or constantly configure every little detail. Instead of manually hardcoding file names, table names, or relationships, you simply follow Laravel’s naming conventions and let the framework handle the rest.

**Why stick to Laravel’s naming conventions?**

1. **Less hassle**: Laravel figures out models, tables, and relationships for you.
    
2. **Cleaner, friendlier code**: Easy to read today and months later.
    
3. **Fewer silly bugs**: No more “Oops, I named it differently in the code and the database.”
    
4. **Faster work**: Skip the boring configuration steps and get straight to building features.
    
5. **Team harmony**: Everyone instantly understands the structure without a long onboarding session.
    

In this blog, we will explore naming conventions for:

1. Models
    
2. Controllers
    
3. Migrations
    
4. CSS files
    
5. JavaScript files
    
6. Blade templates
    
7. Models, Tables & Relationships
    

### **1\. Models**

Models in Laravel represent your database tables and are stored in `app/Models`.

**Convention:**

* **Singular** name
    
* **PascalCase** (StudlyCase)
    
* No underscores or hyphens
    

✅ **Examples:**

```php
// Good
class User extends Model {}
class OrderDetail extends Model {}

// Bad
class Users extends Model {}
class order_detail extends Model {}
```

📌 **Why singular?**  
Each model represents **one record** in the table (one `User`, one `Order`).

---

### **2\. Controllers**

Controllers handle your application logic and live in `app/Http/Controllers`.

**Convention:**

* **PascalCase**
    
* Always end with `Controller`
    
* Group logically based on purpose
    

✅ **Examples:**

```php
class UserController extends Controller {}
class OrderDetailController extends Controller {}
```

🚫 **Bad:**

```php
class usercontroller {}
class Order_Detail {}
```

📌 **Tip:** Use:

```php
php artisan make:controller UserController
```

to auto-follow conventions.

---

### **3\. Migrations**

Migrations define your database structure and are stored in `database/migrations`.

**Convention:**

* **snake\_case**
    
* Words separated by underscores
    
* Describe the action + table name
    
* Timestamp prefix auto-generated
    

✅ **Examples:**

```php
2025_08_15_120000_create_users_table.php
2025_08_15_120100_add_status_to_orders_table.php
```

📌 **Tip:**

```php
php artisan make:migration create_users_table
php artisan make:migration add_status_to_orders_table --table=orders
```

---

### **4\. CSS Files**

Usually stored in `public/css` or `resources/css`.

**Convention:**

* Lowercase
    
* Hyphen-separated
    
* Descriptive names
    

✅ **Examples:**

```php
main.css
admin-dashboard.css
user-profile.css
```

---

### **5\. JavaScript Files**

Stored in `resources/js` (for build tools) or `public/js`.

**Convention:**

* Lowercase
    
* Hyphen-separated
    
* Use action-oriented names if script is specific
    

✅ **Examples:**

```javascript
app.js
form-validation.js
user-profile.js
```

---

### **6\. Blade Templates**

Stored in `resources/views`.

**Convention:**

* Lowercase
    
* Hyphen-separated
    
* Use folders for grouping
    
* No underscores
    

✅ **Examples:**

```php
resources/views/auth/login.blade.php
resources/views/admin/dashboard.blade.php
resources/views/user/profile.blade.php
```

📌 **Tip:** Use dots in view calls:

```php
return view('admin.dashboard');
```

---

### **7\. Models, Tables & Relationships**

Laravel’s **Eloquent ORM** has default rules for naming tables and relationships.

#### **Models & Tables**

* **Models:** Singular, `PascalCase`
    
* **Tables:** Plural, `snake_case`
    

✅ **Examples:**

| Model | Table Name |
| --- | --- |
| `User` | `users` |
| `OrderDetail` | `order_details` |
| `ProductCategory` | `product_categories` |

If your table doesn’t match the convention, define it:

```php
class UserInfo extends Model {
    protected $table = 'user_information';
}
```

---

#### **Relationships**

Eloquent relationships also follow specific naming:

**One-to-One / One-to-Many**

* Method name: **singular** for one-to-one, **plural** for one-to-many
    

```php
class User extends Model {
    public function profile() {  // one-to-one
        return $this->hasOne(Profile::class);
    }

    public function posts() {    // one-to-many
        return $this->hasMany(Post::class);
    }
}
```

**Belongs To**

* Method name: **singular** (refers to parent)
    

```javascript
class Post extends Model {
    public function user() {
        return $this->belongsTo(User::class);
    }
}
```

**Many-to-Many**

* Pivot table: **alphabetical order**, plural, snake\_case
    
* Method name: **plural**
    

```php
// Table: role_user (alphabetical: role before user)
class User extends Model {
    public function roles() {
        return $this->belongsToMany(Role::class);
    }
}
```

✅ **Pivot Table Examples:**

| Models | Pivot Table |
| --- | --- |
| `User` & `Role` | `role_user` |
| `Post` & `Tag` | `post_tag` |

---

#### **Primary and Foreign Key Naming Conventions in Relationships**

In Laravel, following the right primary and foreign key naming patterns ensures that Eloquent relationships work without extra configuration.

* **Primary Keys** – Each table should have an `id` column as its primary key.
    
* **One-to-Many (O2M)** – The foreign key should be the **singular form** of the related model’s name followed by `_id`.
    
    * Example: If a `Post` belongs to a `User`, the `posts` table should have `user_id`.
        
* **Many-to-Many (M2M)** – Use a **pivot table** named with both related model names in **singular, snake\_case**, arranged in **alphabetical order**, with each containing its foreign key in the same pattern.
    
    * Example: For `User` and `Role` models, pivot table → `role_user`
        
        * Columns: `role_id`, `user_id`
            

By sticking to these naming rules, you can define relationships without specifying custom keys, letting Laravel automatically link your models.

---

### **Final Thoughts**

Following these conventions:

* Makes your Laravel codebase self-explanatory
    
* Allows Laravel’s “magic” features to work without extra configuration
    
* Keeps large projects clean and maintainable
