Laravel Migration

What is Laravel Migration?

Laravel Migration is an essential feature in Laravel that allows you to create a table in your database. It allows you to modify and share the application's database schema. You can modify the table by adding a new column or deleting an existing column.

Why do we need Laravel Migration?

Suppose we are working in a team, and some idea strikes that require the alteration in a table. In such a case, the SQL file needs to be passed around, and some team member has to import that file, but team member forgot to import the SQL file. In this case, the application will not work properly, to avoid such situation, Laravel Migration comes into existence.

Laravel Migration allows you to add a new column or delete the records in your database without deleting the records that are already present.

Environment Configuration

In this topic, we will learn about the configuration and environment files. In the laravel project, the two files are created by the composer automatically, and they are .env and .env.example files. Both the files are present in c:/xampp/htdocs/project_name directory.

Laravel Migration

The above screenshot shows that we have two environment files in a project, i.e., .env and .env.example. Let's understand these files in brief.

.env: The .env file is a file present in a project that contains the various settings in a key-value pair. Within the laravel project code, we can get these settings by using the function env('key').

If we are working in a team, then the .env file is not committed to the application source control. Since each developer requires a different environment configuration to use your application, and it would be at a high-security risk if they apply any change in the .env file. The .env file contains the sensitive credentials which cannot be shared. The rule for working with the git repositories is that .env file is not committed to the repository, so it is included in the .gitignore file.

Laravel Migration

The above screenshot shows that the .env file is included in the .gitignore file.

.env.example: The .env.example file is included in the repository but not in the .gitignore file. It works as an example file so that developers can get to know which key-value pairs are required for an application.

Now, we will see how to use the .env file. Open the database.php file available in the C:\xampp\htdocs\laravelproject\config directory. The given below screenshot shows the content of a database.php file.

Laravel Migration

The above screenshot shows that the database.php file returns an array. It returns a connections[] array, which is returning the connections of different databases such as SQLite, MySQL, etc. In the database.php file, the values are assigned to the variables by using the env(key, value) function.






Latest Courses