Question

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

    }

}
Was it helpful?

Solution 2

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.

OTHER TIPS

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.

Migration files maintains the schema of the tables. Using migration, you may never have to go to phpMyAdmin (except for creating a DB). Once done you can simply run the command 'php artisan migrate' and create the tables from the PHP side itself. Also you will never have to worry about the DB environment (MySql, Posgres, Sql Lite etc) as the migration does not truly depend on the environment to which you are migrating the tables.

Seeding helps in creating, for example, different roles (Admin, User, Editor etc) within your application. You will have to just create the seeder files and run the 'php artisan db:seed' command to populate data to the tables from the php side. Also seeds help in creating test data.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top