Courses

Structuring Databases in Laravel 11

Practice: Change DB Structure in "Live" Project

Summary of this lesson:
- Safely changing relationships
- Managing live database changes
- Implementing migration strategies
- Testing database changes

In this lesson, you will see an example of restructuring the database.

Imagine a scenario:

  1. You have Posts that belong to one Category
  2. At some point, you decide to have multiple categories

So, from one-to-many to many-to-many. But the project is already live. What should you do?


Step 1: What Changes Are Needed?

First, let's identify the behavior changes.

Change 1. When viewing a post, instead of showing a single category, it should list all categories with a foreach loop.

Change 2. When showing posts by a category, the query should also be changed.

Where do we start, and how do we do it safely?


Step 2: Automated Tests

Let's start with a different question: "How will you test everything after you make the changes in the code?"

To answer that question, I have a personal rule of thumb. For making any big structure refactorings in a big project: first, write automated tests.

You need to write tests of all the features and how...

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

You also get:

  • 73 courses
  • Premium tutorials
  • Access to repositories
  • Private Discord