Search code examples
phpmysqllaravel-4migration

Benefit Of Migrations in Laravel


as In laravel we Use Migrations to Create Tables and then Seeders to seed out table, am not getting its benefit, as we can do that in normal way by just going to PHPMYADMIN then what we need to that, as we code many lines for it, but how can we justify those lines of code ?

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateItemsTable extends Migration {

    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('items', function(Blueprint $table)
        {
            $table->increments('id');
            $table->integer('owner_id');
            $table->string('name');
            $table->boolean('done');
            $table->timestamps();
        });
    }

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

}

it is true that migrations are being created by php artisan commands, But what is the benefit of them? as we have an alternate to do that? same for the Seeder files as we code many lines for it

class ItemTableSeeder extends Seeder{

    public function run(){
        DB::table('items')->delete();
    $items= array(
        array(
            'owner_id' => '1',
            'name' => 'Watch The Spectacular Now',
            'done' => True
            ),
        array(
            'owner_id' => '2',
            'name' => 'Watch Avengers',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'Watch The Iron man',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'Divergent',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'Bat Man',
            'done' => False
            ),
        array(
            'owner_id' => '1',
            'name' => 'X-Men Days Of Future Past',
            'done' => False
            )
        );
    DB::table('items')->insert($items);

    }

}

Solution

  • Migrations and seeds are database versioning. Imagine that one day you fall in love with, say PostgreSQL or anything other than MySQL. Then imagine you'd like to do some tests with more than several rows of data.. Would you run PHPMYADMIN's equivalent and insert 100, 1000 or 10000 rows?

    So now check this out:

    // migration
    class CreateCommentsTable extends Migration {
    
        public function up()
        {
            Schema::create('comments', function(Blueprint $table) {
                $table->increments('id');
                $table->string('body');
                $table->integer('author_id')->unsigned();
                $table->integer('post_id')->unsigned();
                $table->timestamps();
            });
        }
    
    // seeder
    class CommentsTableSeeder extends Seeder {
    
        public function run()
        {
            Eloquent::unguard();
    
            $faker = Faker::create();
    
            foreach(range(1, 1000) as $index)
            {
                Comment::create([
                    'body' => $faker->sentence(10),
                    'author_id' => rand(1,20),
                    'post_id' => rand(1,150)
                ]);
            }
        }
    

    Faker is a great tool you can find here: https://github.com/fzaninotto/Faker

    All you need to do now is run artisan migrate --seed.

    Of course there are more advantages than automating seeds, you can alter your tables with migrations in case you want to change your schema and so on.