Laravel migration with seeding

Laravel migration with seeding

  • 14 July, 2018
  • Renish Khunt
  • Laravel
  • 167

Today, We are going to learn about Laravel migration with seeding. Laravel provides awesome migration with seeder functionality. Laravel seeder is very helpful for creating the dummy data for migration table. We can create the migration with dummy data for testing purpose using Laravel seeder.

Let’s say we are creating a new migration for Blog table with sample data.

Note: First of all, I hope you install Laravel if not please follow this steps.

Laravel migration with seeding

Let’s create the migration file using artisan command like

php artisan make:migration create_blog_table

Now, Let’s open the migration file from “database/migrations” then create the blog table like

<?php

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

class CreateBlogTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('blog', function (Blueprint $table) {
            $table->increments('id');
            $table->string('title');
            $table->string('category');
            $table->string('tag');
            $table->string('author');
            $table->timestamps();
        });
    }

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

After creating the migration file for Blog. Let’s create the seeding file for sample data like

Seeding File

php artisan make:seeder BlogTableSeeder

Let’s open the seed file from “database/seeds/” then create sample data like

<?php

use Illuminate\Database\Seeder;

class BlogTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $blogs = [
            [
                'title' => 'First Blog Title',
                'category' => 'PHP',
                'tag' => 'PHP',
                'author' => 'Saroj',
            ],
            [
                'title' => 'Second Blog Title',
                'category' => 'Laravel',
                'tag' => 'Laravel',
                'author' => 'Saroj',
            ],
            [
                'title' => 'Third Blog Title',
                'category' => 'Laravel',
                'tag' => 'Laravel',
                'author' => 'Saroj',
            ],
        ];
        foreach ($blogs as $blog){
            DB::table('blog')->insert($blog);
        }
    }
}

Our migration is ready with seeding. Now, Let’s run the migration and seeder using artisan command like

Migration Run

php artisan migrate

Seeding Run

php artisan db:seed --class BlogTableSeeder

We are complete with creating Laravel migration with seeding data. So, it’s too easy to create sample data using Laravel Seeding.

Write a comment below if you have any question or face any issue.