0% found this document useful (0 votes)
20 views3 pages

Connecting With The Database Using Migration

Uploaded by

Mr BuggyMan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views3 pages

Connecting With The Database Using Migration

Uploaded by

Mr BuggyMan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Connecting with the database

Step 1: Create a Database in MySQL (phpMyAdmin)


1. Open phpMyAdmin
2. Create a New Database:
 In phpMyAdmin, click on the Databases tab at the top.
 Enter a name for your database (e.g., laravel_db) in the Create Database field.
 Click the create button.

Step 2: Create a New Laravel Project


Go to this directory, cd C:\xampp\htdocs. If you’re using XAMPP, use this directory for your
project and create a new laravel project

Step 3: Navigate to the Laravel Project Folder:


cd your-project-name

Step 4: Set up Laravel’s Database Connection


1. Edit the .env File
In the root folder of your Laravel project, open the .env file. This file contains the environment-
specific settings for your application.
2. Configure the Database Settings:
Update the following lines in the .env file to match your MySQL database configuration. Laravel
uses MySQL by default.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1 # Localhost
DB_PORT=3306 # Default MySQL port
DB_DATABASE=laravel_db # The name of the database you created
DB_USERNAME=root # Default MySQL username in XAMPP
DB_PASSWORD= # Default MySQL password (leave blank)

Step 5: Create a Migration to Test the Database Connection


1. Create a Migration for the users Table:

In the terminal, run the following command to generate a migration file for creating a users table

php artisan make:migration create_users_table --create=users

If migration is successful, you can see a file created in this following directory:

database\migrations\0001_01_01_000000_create_users_table.php
2. Edit the Migration File:

(i) Navigate to the generated migration file in database/migrations/ and open it.

(ii) Add the following code inside the up() method to define the structure of the users table:

Schema::create('users', function (Blueprint $table) {


$table->id();
$table->string('name');
$table->string('email')->unique();
$table->timestamps();
});

3. Run the Migration:

Run the following command to create the table in your database:

php artisan migrate

Step 6: Test the Database Connection


1. Create a Route to Test the Database:

Open routes/web.php and add the following route to test if Laravel is properly connected to the
database:

use Illuminate\Support\Facades\DB;
Route::get('/test-database', function () {
// Fetch data from the 'users' table
$users = DB::select('SELECT * FROM users');
return $users; // Show the users data (empty array if no data)
});

2. Visit the Test Route:

(i) Open your browser and go to http://127.0.0.1:8000/test-database.

(ii) If your database is connected properly, it will return an empty array ([]) or any data in the
users table if you have added users.
Extras:
To undo all migration, use the following command:

php artisan migrate:rollback

Fresh Migrate (Delete All Tables and Re-run Migrations)

If you want to reset your database and re-run all migrations (warning: this will delete all data in
your database), you can use the migrate:fresh command.

1. Run the Fresh Migrate Command:

php artisan migrate:fresh

This command will drop all existing tables and re-run all migrations from scratch.

You might also like