"Day2 Laravel Assignments: Installing XAMPP, Laravel, and Configuring Your First Database Connection"
Day2 Laravel Assignments
Installing XAMPP, Laravel, and Configuring
Your First Database Connection
Assignment 1: Install XAMPP and Laravel
Objective:
This assignment will help students practice the installation of XAMPP and Laravel, as well as the initial configuration of the Laravel environment.
Instructions:
1. Install XAMPP:
o Download and install XAMPP from https://www.apachefriends.org/index.html.
o Start the Apache and MySQL services from the XAMPP Control Panel.
o Verify that the installation is successful by visiting http://localhost/ in your web browser. You should see the XAMPP welcome page.
2. Install Composer:
o Download Composer from https://getcomposer.org/download/.
o Follow the instructions to install Composer on your system.
o After installation, verify it by running composer --version in your terminal or command prompt.
3. Create a New Laravel Project:
o Open your terminal or command prompt.
o Navigate to the folder where you want to install Laravel (e.g., C:\xampp\htdocs).
o Run the following command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel my-laravel-app
o After the installation is complete, navigate into the project folder:
cd my-laravel-app
4. Start the Laravel Development Server:
o Run the Laravel development server using the following command:
php artisan serve
o Once the server starts, open your browser and go to http://127.0.0.1:8000. You should see the Laravel welcome page.
Step-by-Step Solution:
1. Install XAMPP:
o Go to the XAMPP website and download the appropriate version for your operating system.
o After downloading, open the installer and follow the steps to install XAMPP. Ensure that Apache and MySQL are selected during installation.
o Once installed, open the XAMPP Control Panel and start Apache and MySQL.
o To verify the installation, open your browser and go to http://localhost/. You should see the XAMPP welcome page.
2. Install Composer:
o Go to the Composer website and download the installer for your system.
o Run the installer and follow the steps to install Composer.
o After installation, open your terminal or command prompt and run:
composer --version
This should display the installed version of Composer. If you see a version number, Composer is installed correctly.
3. Create a New Laravel Project:
o Open the terminal and navigate to the directory where you want to install Laravel. For example:
cd C:\xampp\htdocs
o Run the following Composer command to create a new Laravel project:
composer create-project --prefer-dist laravel/laravel my-laravel-app
This command will download Laravel and its dependencies into a folder named my-laravel-app.
4. Start the Laravel Development Server:
o After the installation is complete, navigate into the Laravel project directory:
cd my-laravel-app
o Run the following command to start the Laravel development server:
php artisan serve
o If everything is set up correctly, you will see a message like:
Laravel development server started: http://127.0.0.1:8000
o Open your browser and go to http://127.0.0.1:8000. You should see the Laravel welcome page.
Assignment 2: Configure the Database Connection in Laravel
Objective:
This assignment will teach students how to configure the .env file for database connections in a Laravel project.
Instructions:
1. Create a New Database:
o Open phpMyAdmin (http://localhost/phpmyadmin).
o Create a new database named laravel_db.
2. Configure the .env File:
o Open the .env file located in the root directory of your Laravel project.
o Modify the database connection settings to match the following:
§ DB_CONNECTION=mysql
§ DB_HOST=127.0.0.1
§ DB_PORT=3306
§ DB_DATABASE=laravel_db
§ DB_USERNAME=root
§ DB_PASSWORD=
3. Run the Database Migration:
o Run the following command in the terminal to apply the default Laravel migrations and test the connection:
php artisan migrate
o If the migration runs successfully, it means the database connection is correctly configured.
Step-by-Step Solution:
1. Create a New Database:
o Open phpMyAdmin in your browser by navigating to http://localhost/phpmyadmin.
o Click on the "Databases" tab and create a new database called laravel_db.
2. Configure the .env File:
o Open the .env file inside your Laravel project folder (my-laravel-app).
o Find the database configuration section, which looks like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
o Change the DB_DATABASE value to laravel_db (the database you just created), and leave DB_USERNAME=root and DB_PASSWORD= as they are by default.
o The final configuration should look like this:
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel_db
DB_USERNAME=root
DB_PASSWORD=
3. Run the Database Migration:
o In the terminal, run the following command to apply the default migrations:
php artisan migrate
o If everything is set up correctly, you should see a message like:
Migration table created successfully.
o This indicates that Laravel successfully connected to your MySQL database and applied the migrations.
Assignment 3: Create a New Laravel Project and Configure Database
Objective:
This assignment will teach students to create a Laravel project, configure the database, and set up a basic route to interact with the database.
Instructions:
1. Create a Laravel Project:
o Follow the steps from Assignment 1 to create a new Laravel project using Composer.
2. Set Up Database Configuration:
o Follow the steps from Assignment 2 to configure the .env file for database connection.
3. Create a Basic Route:
o Open routes/web.php and add a new route:
Route::get('/test-db', function () {
$users = DB::table('users')->get();
return view('users', ['users' => $users]);
});
o Create a resources/views/users.blade.php file with the following content:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Users</title>
</head>
<body>
<h1>Users List</h1>
<ul>
@foreach($users as $user)
<li>{{ $user->name }}</li>
@endforeach
</ul>
</body>
</html>
4. Run the Laravel Development Server:
o Run the Laravel development server with php artisan serve.
o In your browser, go to http://127.0.0.1:8000/test-db. You should see a list of users from the users table in your database.
Step-by-Step Solution:
1. Create a Laravel Project:
o Follow the instructions from Assignment 1 to install Laravel.
2. Set Up Database Configuration:
o Follow the steps from Assignment 2 to configure the .env file for database connection. Make sure the database is created and configured correctly.
3. Create a Basic Route:
o Open routes/web.php and add the following code to create a route for displaying the users:
Route::get('/test-db', function () {
$users = DB::table('users')->get();
return view('users', ['users' => $users]);
});
o This code fetches all the records from the users table and passes them to a Blade view named users.
4. Create the Blade View:
o In the resources/views folder, create a file named users.blade.php and add the following code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Users</title>
</head>
<body>
<h1>Users List</h1>
<ul>
@foreach($users as $user)
<li>{{ $user->name }}</li>
@endforeach
</ul>
</body>
</html>
o This Blade template will display a list of user names.
5. Run the Development Server:
o Run the server with the following command:
php artisan serve
o Open your browser and go to http://127.0.0.1:8000/test-db. If everything is configured correctly, you should see a list of user names from the users table.

Comments
Post a Comment