DEV Community

Cover image for Migration Files(Laravel Example)
Mohammad ALi Abd Alwahed
Mohammad ALi Abd Alwahed

Posted on

Migration Files(Laravel Example)

Long post, read it if you're interested in delving deeper into understanding Laravel migration files and their function in general.

It's really important to understand the technology you're using as much as you're able to use it. So what does migration mean? In Laravel or in any other framework, why do we use it, or what's the benefit of using it?

Back in the days of the dinosaurs, when we worked on a project and made changes to the database or created new databases for the first time on the server, we had to bring in the SQL commands and spread them everywhere or import them.

And every time we made a change locally, we had to execute the command on the server, for example. Or when we worked in a team and programmer A made changes to the database, he had to send the entire command or database to programmer B to see the changes.
In addition to the complexity of SQL, this is where the main task of migration comes in, which allows us to save our modifications to PHP files and upload them with the project in a comprehensible language. As an example of its use, I made changes to the user table in a migration file. My colleague does a pull, and he pulls the changes and does the command:
php artisan migrate
Which pulls our files in order by date and bypasses the commands that have already been executed before, so his database matches mine, as well as the server.

Of course, the connection process to the data with these files is through the Migration class, which the files extend, and it provides us with a large helper library through the Schema class and the larger builder class that is translated through the Blueprint class.

It makes writing SQL easy for us. In addition to this, you can write SQL natively through DB::statement inside any migration file, and the command will be applied normally.

Obviously, we can't write about migration without explaining the two main functions in it: up and down.

The up function runs the command that is translated from this file to the SQL language, and the down function rolls back to the last command that was executed.

That's it, folks!

Top comments (0)