আগামী ১৫ ডিসেম্বর -২০১৭ তারিখ থেকে শুরু হচ্ছে পাঁচ মাস ব্যাপী Professional Web Design and Development with HTML, CSS PHP,MySQL,JavaScript, AJAX, JQUERY, Bootstrap and Live Project কোর্সের ৮৭ তম ব্যাচ এবং ২৬ ডিসেম্বর-২০১৭ তারিখ থেকে শুরু হচ্ছে চার মাস ব্যাপী Zend PHP-7 Certified PHP Engineering (Advance PHP) কোর্সের ৩৫ তম ব্যাচ। প্রত্যেকটি কোর্স এর ফী নির্ধারণ করা হয়েছে ৩০,০০০/= আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করতে অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল: 01722 81 75 91

Getting Started With Laravel – Uses of Migration

Hello everybody. We’re here to learn Laravel Framework again. Those who missed the the other post on Laravel basic please do follow those post because without those post you’re not going to understand what am I want to say in here. You can have those post just in the link below:

Learn Laravel

Today we’re going to learn database work in Laravel. We’re not going to use the CRUD here but we’re going to learn about this amazing module called Migrations. May be you want to say something here why use this Migrations thing? Think of migrations as a type of version control for your database. Let’s say you’re working on a team, and you all have local databases for development. Now some developer makes a change to the database & checks in his code that uses the new column. You pull in the code, and your application breaks because you don’t have the new column. What do you do? Migrations are the answer. Let’s dig in deeper to find out how to use them!

So now what we want to do is to install Migrations in our local database via Artisan. To do that first off all go to your project directory through the command line & use the code below to install Migrations. You may also want to see the picture below.

php artisan migrate:install

install migration

Now as you can see the installation completed, it’s time for you to check the database as if it’s really installed or not go to the phpmyadmin browse through your database & check if there’s a table created named “laravel_migrations” see the picture below for the example.

migration table after installation

It’s time for us to create some basic table with Migrations. To do that we need to go back to command prompt and write down the below command just like the picture below:

php artisan migrate:make create_authors_table

creating a table using migration

Now check into your source code go to the migration folder into application->migration there’s already a file created with your given table name with the timestamp at the beginning of the file name. Just go to the file & see what’s in there, see the picture below:

in the src directory mig table

In the picture above we can see that there is a class named as your file name with two methods up & down. We’ve to give the instruction in there, if we want to create a table we would like to code in the UP method & if we want to drop the table we’ll code in the DOWN method. Now it’s time for us to create a table and for that edit the migration file that you’ve created just now. For example just copy the below code & see.

class Create_Authors_Table {

/**

* Make changes to the database.

*

* @return void

*/

public function up()

{

Schema::create('authors',function($table){

$table->increments('id');

$table->string('name');

$table->text('bio');

$table->timestamps();

});

}

/**

* Revert the changes to the database.

*

* @return void

*/

public function down()

{

Schema::drop('authors');

}

}

Here in this code what we’ve done in the UP method is that we’ve used laravel’s Schema class & it’s create method to create a table. This method has two parameters first one is for the table name & second one is a function where the table name passed as an argument, in that function we should write down the attributes of that table like I did in the body of the function. There is an Id field with increment, a name field which one is a string type, bio field which can be bigger that’s why it’s in text type & used timestamp. This is the UP method that we’ve coded.

Now to drop this table we just have to use the Schema class & it’s method called drop and passed the table name as the parameter in that method that’s it.

Now to put the table in the database we need to go to the command line & use the code below to create a table into the database.

php artisan migrate

The both of the picture below to be sure about the creation of the table. Go to the phpmyadmin refresh your page & see if there’s a table named authors created or not like the picture below:

create a table

in phpmyadmin after creating a author table

Now it’s time for inserting some data in table. For that we need to do as same as to create a table for migration. Go to the terminal and write down the below code for the migration file:

php artisan migrate:make add_authors

After this command you should have a file name add_authors into your migration folder with the timestamp at the beginning. It’s time for us to edit that page also. For that copy the code below & paste it into your add_authors file.

class Add_Authors {

/**

* Make changes to the database.

*

* @return void

*/

public function up()

{

DB::table('authors')->insert(array(

'name' => 'Adnan',

'bio' => 'I am good actually',

'created_at' => date('Y-m-d H:m:s'),

'updated_at' => date('Y-m-d H:m:s')

));

DB::table('authors')->insert(array(

'name' => 'tisha',

'bio' => 'Shes also good actually',

'created_at' => date('Y-m-d H:m:s'),

'updated_at' => date('Y-m-d H:m:s')

));

}

/**

* Revert the changes to the database.

*

* @return void

*/

public function down()

{

DB::table('authors')->where('name','=','Adnan')->delete();

DB::table('authors')->where('name','=','tisha')->delete();

}

}

As you can in the code above for the table creation we used Schema class but for insertion a data we’re using DB class and it’s static method called table. In this method we just have to pass a parameter which will be the table name of the database & then use insert method to insert the data. You may noticed that we’ve created an attribute called timestamp now in the above code we’re inserting created_at & update_at attributes in the timestamp area & on these attributes we’re simply using php date function to put the actual server date & time in the database. Now check your database in phpmyadmin if there’s any data inserted or not just like the picture below:

inserted data

Now if we want to drop the info or table we can use it via migration rollback. Check this out on your command line just put the below code:

php artisan migrate:rollback

If you enter this command than the info will be rolled back, then if you again put this command the table will be rolled back. Now if you want to reset the whole migration just put the code below:

php artisan migrate:reset

The above code will reset the whole migration in the database. If you want set the database again just put the below code so that you can have your earlier data.

php artisan migrate

Now that’s it for today’s tutorial. Hope that you’ve understood migration very well. If not please leave comment here so that I can give you the proper explanation. And most important thing is that don’t forget to visit the Laravel Documentation of Migrations.

An enthusiastic of work with over 2+ years of experience. Extensively experienced on wordpress, Codeigniter, Laravel.

##Visit my github page (https://github.com/ashawkat)
##Visit my blogging page where I spread out my knowledges around all other people (http://w3programmers.com/author/tanim)

If you want to hire me then you can contact me via email or skype.

Skype: ashawkat89
Email: [email protected]

4 comments on “Getting Started With Laravel – Uses of Migration
  1. Thx for tut with class Add_Authors.
    How about dumping table from phpmyadmin into class Add_Authors?
    I have a table with data, but want to create migration to create that table with that data. Is there any tool or function to do that?

    Best regards.

    • you’ve to just rollback the migration couple of time to dump the table from the database or you can just reset the migration like i’ve told you before !! Now as you’ve asked if you can add the data alongside with the table answer is yes. You can add the table that you want to insert via migration & then just add the data of your table via migration also !! if you talk about don’t using migration system just the old style you can just import the data then you can browse them by getting the data via model, controlling them & showing them on the view using routes. If you don’t even understand what I’ve told you now please wait for my crud system. you’ll get to know everything ok !! stay tuned for that topic !!

Leave a Reply

Your email address will not be published. Required fields are marked *