Migration Database in Laravel

hendra kusuma
Zetta Tech
Published in
4 min readSep 16, 2023

Hello everyone, back again with me. I want to share knowledge about the concept of Migration Database in the Laravel framework,

Background
In this case, currently, I’m on learning the PHP backend framework Laravel on my Bootcamp,

Task

I want to share about migration database and how to use it in Framework Laravel

Getting Started

Database migration is the process of transferring the table structure within the database from the model file to a database management application (Database Management Tools) automatically designed to facilitate the management and use of databases. Here, I am using the Tableplus application. Firstly, I will start by creating a migration file. Previously, in the migration folder, there is a default Laravel migration file that is automatically generated when Laravel is installed or built-in like this.

default migration file Laravel

However, we can also create a migration file according to the database structure defined in the model file we want to create. First, we can create a migration using a command like this: `php artisan make:migration create_books_legend_table`. After running this command, the migration file will be automatically generated in the migrations folder.

Automation Generate Migration File

Here we can see the code contents, where in the schema, it's where we input data that is almost identical to what's in the model file. It is important to pay attention to the data types of each column name, for example, like this.

Example Data Migration

Don’t forget that I hope you have also understood the use of the database application and have already connected the database to your VSCode configuration beforehand. Here, I am using the TablePlus application, and for the local database server, I am using Laragon. The .env file can be adjusted to match your database settings when installing MySQL.

setting on file .env

After configuring the migration files, we can proceed with the migration using the following command: “php artisan migrate.” Automatically, all the migration files will be copied to the MySQL database, and you can verify them yourself through TablePlus.

Example view data from Tableplus

If you want to roolback, you can also roll back using the command “php artisan migrate:rollback.”

Seeder

A Seeder file serves the purpose of creating dummy data for testing purposes during the development of this application. It is usually used to test whether the API functions as intended. Seeder files are essential as they can expedite the application development process. Here’s how to create a seeder file: use the command “php artisan make:seeder SeederName.” In the “SeederName” section, you can provide a name for your seeder, for example, “php artisan make:seeder BookSeeder.” This command will automatically generate it. Below is an example of the content of a seeder file.

Example Data Seeder

The seeder file can be configured accordingly, for example, I added only 3 data to the books_legend table. After that, we can register the seeder file in the DatabaseSeeder.php file so that when the seeder is processed, the data can be inserted. So when executed with the “php artisan db:seed” command, it will automatically populate the database. Similarly, if we have multiple seeder files to populate multiple tables, with one “php artisan db:seed” command, all the data will be filled in the tables.

Register your Seeder here

And when checked in TablePlus, the seeder data is automatically populated. When initially checked, it needs to be refreshed.

Data Table Books Legend succesfuly inserted seeder

okay thanks for your read, see you in the next post :)

Github :https://github.com/Hendra-Kusuma
Email:
hendrakusuma.vegas@gmail.com

--

--