📖 Scenario: You are building a simple Laravel application to manage books and authors. Laravel uses conventions to name database tables automatically based on model names.
🎯 Goal: Learn how to create tables with proper Laravel naming conventions and understand how Laravel links models to tables.
📋 What You'll Learn
Create a model called Book and understand its default table name
Create a model called Author and understand its default table name
Create a custom table name for a model
Use Laravel's migration files to define tables with correct names
💡 Why This Matters
🌍 Real World
Most Laravel applications use these naming conventions to keep code clean and predictable when working with databases.
💼 Career
Understanding Laravel's table naming conventions is essential for backend developers working with Laravel to build scalable and maintainable applications.
Progress0 / 4 steps
1
Create a Book model and migration
Run the artisan command to create a model called Book with a migration file. The migration will create a table named books by Laravel's default naming convention.
Laravel
Hint
Use php artisan make:model Book -m to create both model and migration.
2
Create an Author model and migration
Run the artisan command to create a model called Author with a migration file. Laravel will create a table named authors by default.
Laravel
Hint
Use php artisan make:model Author -m to create both model and migration.
3
Create a model with a custom table name
Create a model called Person with a migration. Then, inside the Person model file, set the protected property $table to 'people' to override Laravel's default table name.
Laravel
Hint
Override the table name by adding protected $table = 'people'; inside the model class.
4
Define migration files with correct table names
Open the migration files for books, authors, and people tables. Ensure the Schema::create method uses the correct table names: books, authors, and people respectively.
Laravel
Hint
Check each migration file uses Schema::create with the correct table name.
Practice
(1/5)
1. In Laravel, what is the recommended naming convention for database tables?
easy
A. Plural, lowercase, and use underscores (snake_case)
B. Singular, uppercase, and use camelCase
C. Plural, uppercase, and use hyphens
D. Singular, lowercase, and use spaces
Solution
Step 1: Understand Laravel's default table naming
Laravel expects table names to be plural and lowercase with underscores separating words (snake_case).
Step 2: Compare options with Laravel conventions
Only Plural, lowercase, and use underscores (snake_case) matches plural, lowercase, and underscores. Others use singular, uppercase, or invalid characters.
Final Answer:
Plural, lowercase, and use underscores (snake_case) -> Option A