Courses

Structuring Databases in Laravel 11

Naming Things in Databases: 4 Tips

Summary of this lesson:
- Following Laravel naming conventions
- Managing table and column names
- Handling reserved SQL words
- Implementing naming best practices

Usually, when creating Models and Migrations, you should stick to Laravel's naming convention and use English words. Then, Laravel will try to guess names. In this lesson, I will advise you on choosing/customizing names in Laravel DB.


Break Default Table Names Convention? Careful.

For example, if you call your Model Role, Laravel will know its table should be roles. But what if names are different?

You can set the table names on the Model in the protected $table property. So, if you have a singular role for the table name, you would set it in the Role Model.

class Role extends Model
{
protected $table = 'role';
 
// ...
}

When using a belongs-to-many relationship, a pivot table should be created with the two tables' singular names in alphabetical order. For the users and roles relationship, it would be role_user and would contain foreign keys to both tables.

Usually, foreign key fields are named with the...

The full lesson is only for Premium Members.
Want to access all 18 lessons of this course? (81 min read)

You also get:

  • 69 courses (majority in latest Laravel 11)
  • Premium tutorials
  • Access to repositories
  • Private Discord