Wednesday, 13 July 2016

Migrations (Creating Project)

Creating a project
First of all open the command prompt and go to location where you want your project to be e.g. D:>\wamp\www
Then write the following line
composer create-project laravel/laravel bistpedia

bitspedia is name of my project, you can give any name.
Before we talk about migrations we need to set few things:

Go to .env file located in your project and change the following code as

DB_HOST=localhost

DB_DATABASE=bitspedia

DB_USERNAME=root

DB_PASSWORD=1234

Remember that bitspedia is name of my database, user name and password of mysql is root and 1234 respectively, change this code according to your setting.

Then open databse.php file located in config folder and change the following code as

'mysql' => [

    'driver'    => 'mysql',

    'host'      => env('DB_HOST', 'localhost'),

    'database'  => env('DB_DATABASE', 'bitspedia'),

    'username'  => env('DB_USERNAME', 'localhost'),

    'password'  => env('DB_PASSWORD', '1234'),

    'charset'   => 'utf8',

    'collation' => 'utf8_unicode_ci',

    'prefix'    => '',

    'strict'    => false,

],

Remember that I am using mysql so I changed in this and as mentioned above bitspedia is name of my database, user name and password of mysql is root and 1234 respectively change this code according to your settings.
MIGRATIONS
Migrations are just a php source file. Have you ever faced a problem of creating, deleting and specially updating a database, here is the solution. It creates deletes and updates database using php code, so that all team members have same database of their project. It contain two functions up() and down(). Up is used to make some change in database while down is used to remove or undo that change in database.
Now if you go to the migration folder located in database, you will see two files one named create_users_table and other named create_password_resets_table with time-stamped in there beginning, these are already created migrations so if you don’t need them you can just delete them. So we will delete them and create our own migration.
Let’s create a migration.
We will use an Artisan command line interface
. Write the following line in command prompt
php artisan make:migration create_users_table
This will make a php file in migrations folder named something like this 2016_01_21_203540_create_users_table, migrations are time-stamped so they are always executed in order.
Now open this file. You can see that it has created a class named CreateUsersTable
so class name is good it tells what the purpose of class is. You can see two functions in it up() and down(). As mentioned above the up() is used to make some change so let’s make first change in our database. We want to create a table named “users” with attributes id, name and email. And as mentioned above the down() is used to undo the change so in this case we just want our down function remove this table so we will update our up and down functions as following

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->string('email');
    });
}
public function down()
{
    Schema::drop("users");
}
You can use a variety of column types when building your tables.
Just writing this will not do anything so we need to run these migrations, for that write the following line in your command prompt.
php artisan migrate
Now check you database you will see two tables and one of them is what you wanted to create i.e. users. If you check the structure of that table you can see that it contains three columns i.e. id, name and email. Remember that laravel keeps record of migrations so every time you run migrations it will only run new migrations and will not repeat any same migration again.

Now let’s make a change in our table and add a fatherName field in our users table. Remember that always use a new migration to alter your table. So we will create new migration
php artisan make:migration add_field_in_users_table

Then we will change its methods as following:

public function up()

{

    Schema::table('users', function ($table) {

        $table->string('fatherName');

    });

}

public function down()

{

    Schema::table('users', function ($table) {

        $table->dropColumn('fatherName');

    });

}
 
And then run this migration

php artisan migrate
If you have changed in your existing migration for some reason, you can use
php artisan migrate:refresh
This will first run the down() off all migrations and then run up() of all of them.
If you want to undo the last migration you can use
php artisan migrate:rollback
If you want to undo all the migrations you can use
php artisan migrate:reset


No comments:

Post a Comment