Introduction
Hi friends, this is the next part of the Laravel tutorial series. And in this part, we are going to discuss how to make a MySQL database connection. After making a database connection in a Laravel application, then after we will see how to create a MySQL table in phpMyAdmin from this Laravel application.
In the previous part, we have seen how to install Composer PHP dependencies for the Laravel framework and then after how to download a Laravel application by using Composer.
Configuring the Database Connection: The .env File
So now let's start discussing how to make a database connection. So here we have already made one laravel_testing database. In this database, we have not created any table. So we will make a connection with this database in Laravel.
For making a database connection, we have to define database connection properties in two files. So this is our downloaded Laravel application directory structure. For making a database connection, first, we have to define database connection properties into this .env file. After clicking on this file, for making a database connection, here we can see DATABASE_CONNECTION is equal to mysql. So here we define HOST is equal to localhost, DATABASE is equal to laravel_testing, USERNAME is equal to root, and PASSWORD is equal to blank. This is the first file in which we can set this database connection configuration setting.
Verifying Configuration in config/database.php
To define database configuration in this file, now we have to go to the config folder, and in this, here we can see database.php file. So this is the second file in which we want to define database connection credentials. So we have opened this file, and under this file, we can see different database connection settings, but in this, by default, it's set to MySQL database connection.
But suppose you have used another database, SQLite, so you can change this name from mysql to sqlite and define that database connection credential. But here, we want to make a MySQL database connection, so here we can see MySQL database connection setting. So in the host key, we have set localhost. In the database key, we have set laravel_testing database name. In the username key, we have set root, and in the password key, we have set a blank value.
So this way we can define MySQL database settings in the Laravel framework. So in this framework, we want to define database settings in two files, otherwise we will get a database connection error.
Generating a Model and Migration with Artisan
After making the MySQL database connection, now we want to create a simple MySQL table in phpMyAdmin from this Laravel application. So for this, first, we have to go to the database folder, and in this, there are three subfolders like factories, migrations, and seeds. So we have to go to the migrations folder. Under this folder, we can see some files, so first, we have removed that file. And after removing, we go to the command prompt in which we have run Composer.
Here we have already come into our Laravel application. If you are not in the Laravel application, then your artisan command will not run. But here, we are already in the Laravel root directory and here we have the right command like: php artisan make:model Student -m.
This command will generate two files. One is a migration file for creating the student table, and another is the Student model of this Laravel application.
Defining the Table Schema in the Migration File
So here we can see the create_student migration file in the migrations folder, and in the app folder, we can see the Student.php model file. We will discuss this file in the next video tutorial, but first, we go to the migrations folder and open the create_student_table migration file. After opening this file, we can see there is some code already written in this file. This is the CreateStudentsTable class which extends the Migrations class. And under this, we can see two methods: up for creating the student table and migrating it into phpMyAdmin, and the second method for reversing the migrate, that means dropping the students table.
In the up method, we can see the student table structure with three columns already created. Because by using this Schema class with the create method, we can build a database table. This code will make a students table with three columns like id with auto-increment and primary, and $table->timestamps() method will create two table columns like created_at and updated_at with datatype timestamp.
But we want to add two more columns like first_name and last_name. So here we write $table->string('first_name');. It will make a first_name table column with datatype VARCHAR(255) character.
Same way for the last_name table column, we again write $table->string('last_name');. This code will also make a last_name table column with data type VARCHAR(255) character.
Running the Migration to Create the Table
After this, now we want to migrate this student table to phpMyAdmin. So we go to the command prompt and here we write php artisan migrate command and press enter. And we have received a message like 'student table has been migrated'. That means the student table successfully migrated and it will make a student table in the laravel_testing database. So we go to the laravel_testing database and in this, we can see the student table with five columns like id, first_name, last_name, created_at, and updated_at has been created.
Conclusion and Next Steps
So, this way we can create a table in a MySQL database by using this Laravel application. So in this video tutorial, we have discussed how to make a MySQL database connection with a Laravel application, and after making the database connection, how to migrate a table from a Laravel application to phpMyAdmin. We will use this student table for discussing how can we insert, update, delete MySQL table data in a Laravel application. In short, we will make a CRUD operation application in the Laravel framework in the next part.
If you have any query regarding this video tutorial part, please comment your query in the comment box. And if you liked this video tutorial, please share with your friends or even you can also share on social media also. If you want to get more updates regarding our video tutorial, please subscribe to our YouTube channel to get more updates regarding the release of future videos. Lastly, keep watching our YouTube channel. Thanks for watching.