Database Configuration and Migrations
# CHAPTER 8
Database Configuration and Migrations
1. Introduction
In core PHP development, if you wanted to create a database table, you opened phpMyAdmin, clicked around the GUI, and manually added columns. If you worked on a team, every developer had to manually recreate the database on their own laptop. It was a nightmare. Laravel solves this with Migrations. Migrations are like "version control" (Git) for your database. In this chapter, we will learn how to build databases entirely through PHP code.2. Learning Objectives
By the end of this chapter, you will be able to:-
Configure the
.envfile to connect to MySQL.
- Generate and read Migration files using Artisan.
- Use PHP to define database schemas (tables and columns).
- Execute migrations to automatically build the database.
3. Beginner-Friendly Explanation
Imagine giving instructions to a construction crew to build a house. The old way (phpMyAdmin) is driving to the site, grabbing a hammer, and building the house yourself. If your friend wants the exact same house, they have to come over and copy everything you did manually. Migrations are the Blueprint. You write a blueprint file in PHP that says: "Build a room called 'Users', add a window called 'Email', and a door called 'Password'." You hand the blueprint to the Artisan robot. The robot builds the house instantly. If your friend downloads your code, they give the same blueprint to their robot, and they get the exact same database built on their computer in two seconds.
4. Step 1: Configuration (.env)
Before migrating, Laravel needs to know where the database is.
Open your .env file and ensure the credentials match your local MySQL server (XAMPP).
5. Step 2: Creating a Migration File
We want to create a database table to store Blog Posts. Open your terminal and use the Artisan command:Laravel will create a new file in the database/migrations/ directory. The filename will have a timestamp (e.g., 20240101000000createpoststable.php). The timestamp ensures migrations run in the exact order they were created.
6. Step 3: Writing the Schema
Open the migration file. You will see two methods:up() (what happens when we build) and down() (what happens if we undo the build).
Let's design the posts table:
7. Step 4: Running the Migration
The blueprint is written. Now we tell the Artisan robot to build it. In your terminal, run:Laravel connects to your MySQL database, translates your PHP code into raw SQL, and creates the tables perfectly! If you open phpMyAdmin, you will see your new posts table.
8. Rolling Back (Undoing Mistakes)
If you made a typo (e.g., you spelled 'title' as 'titel'), do not go into phpMyAdmin to fix it! You tell Artisan to undo the last action:Then, you fix the typo in your migration PHP file, and run php artisan migrate again.
9. Best Practices
-
Never Modify Old Migrations: Once a migration has been pushed to a live production server, you *never* edit that file again. If you need to add a new column later, you create a *new* migration (
php artisan make:migration addimagetopoststable) that updates the existing table.
10. Common Mistakes
-
SQLSTATE[HY000] [1049] Unknown database: If you run
php artisan migrateand get this error, it means you typedDBDATABASE=myappin your.envfile, but you forgot to actually open phpMyAdmin and click "New" to create the blankmyappdatabase first.
11. Exercises
- 1. Explain why Migrations are a superior method for database creation when working on a team of developers, compared to manually using a GUI tool like phpMyAdmin.
12. Coding Challenges
-
Challenge: Use Artisan to create a migration for a
commentstable. Add columns forbody(text),userid(integer), andpostid(integer). Run the migration.
13. MCQs with Answers
What is the primary purpose of Laravel Migrations?
Which terminal command executes all pending migration files and physically creates the tables in the MySQL database?
14. Interview Questions
-
Q: Walk me through the purpose of the
up()anddown()methods in a Laravel migration file.
- Q: Explain how you would safely add a new column to a table that has already been migrated to a live production database with thousands of users.
15. FAQs
Q: What is thetimestamps() method?
A: timestamps() is a massive time-saver. By adding it to your migration, Laravel automatically creates a createdat and updated_at column in your table. Whenever you insert or update data using Laravel's ORM, Laravel updates these timestamp columns automatically!