Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);

    }

}
like image 296
Junaid Farooq Avatar asked May 03 '14 12:05

Junaid Farooq


2 Answers

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.

like image 172
Jarek Tkaczyk Avatar answered Sep 27 '22 17:09

Jarek Tkaczyk


The main benefit is that you will do it in your development server/station, and you may change the schema many times in development, migrate, rollback migrations, and re-migrate them, and as soon as your application id done, you don't have to remember what you have to do in your production environment, Laravel will do it automatically for you.

Using PHPMyAdmin, you would have to create tables and edit fields manually locally and in your remote server, and you would risk forgetting something and breaking your application. And, if you have more than one server serving your application, the problem is bigger.

like image 24
Antonio Carlos Ribeiro Avatar answered Sep 27 '22 17:09

Antonio Carlos Ribeiro