Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Laravel migrations work?

I'm totally new to this type of framework. I've come from barebones PHP development and I can't seem to find an easy to understand guide what migrations actually do.

I'm trying to create a project that already has an existing database. I've used this: https://github.com/Xethron/migrations-generator[1] but making changes to the schema via the migrations seems to spit out errors which means I have no idea what I'm doing.

I really need a simple run down of what migrations actually do, how they affect the database and anything else you think would help an absolute beginner.

like image 934
Recur Avatar asked May 13 '15 16:05

Recur


2 Answers

Migrations are a type of version control for your database. They allow a team to modify the database schema and stay up to date on the current schema state. Migrations are typically paired with the Schema Builder to easily manage your application's schema.

With migrations you don't need to create table in phpMyAdmin, you can do it in Laravel. Here is an example to create a user table:

class CreateUsersTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('users', function(Blueprint $table)
        {
            $table->increments('id'); // autoincrement id field
            $table->string('name');   // string field
            $table->string('lastname');
            $table->string('title');
            $table->string('email')->unique();   // unique string field
            $table->string('password', 60);      // string field with max 60 characters
            $table->boolean('Status')->default(0); // string field with default value 0
            $table->timestamps();

        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('users');
    }

}

I this code we create table with fields like "name", "lastname"... we said in our Laravel code they are string type when migration is done we have complete table in databese with this fields.

Run a migration to create table

To create a migration, you may use the make:migration command on the Artisan CLI (artisan command line interface):

php artisan make:migration create_users_table

or

php artisan make:migration create_users_table --create=users

Run a migration to alter table

When you need to do some changes in database table example: add field vote to user table you can do like this in your Laravel code without touching SQL code

php artisan make:migration add_votes_to_users_table --table=users

Rollback the last migration operation

If you make mistake and did something wrong you can always rollback to return database in previous state.

php artisan migrate:rollback

Rollback all migrations

php artisan migrate:reset

Rollback all migrations and run them all again

php artisan migrate:refresh

php artisan migrate:refresh --seed

One of best advantage of migrations are creating database without touching SQL code. You can make whole database with relationship in PHP code then migrate it into MySQL, PL/SQL, MSSQL or any other database.

Also I recommend the free Laravel 5 fundamental series, in episode 7 you can hear more about migrations.

like image 132
Adnan Avatar answered Sep 29 '22 19:09

Adnan


Imagine you are working on a project with other developers. As developers are adding more and more features, they are also required to add more and more tables, columns, remove columns, change column data types, etc...

This can very quickly and easily get out of control. If you missed some SQL from one developer, another developer's SQL may not work correctly. It also potentially becomes a huge time waster trying to sort through a bunch of sql files trying to figure out which ones you've missed. It's only a matter of time till everyone on the development team is working with different databases or god forbid, someone breaks the production database. With migrations, you just need to run php artisan migrate in the command line and all changes to the database will be taken care of for you.

This is basically why migrations are useful I'm not going to go into the basics of how they work because Kryten has a pretty good write-up here already.

like image 45
user1669496 Avatar answered Sep 29 '22 18:09

user1669496