How to Use Seeder in Laravel to Populate Database?

Laravel Seeder is good way to populate database automatically by code. Sometimes we need that. In a perfect world users or admins add values in database and our code just enables them but sometimes we add values in Database by Seeders. For Example, we want to add few leads in our database by running some code and we only have those leads in Array form.

So first we will start by making a seeder by running this command.

php artisan make:seeder LeadsSeeder

This will create a file at /database/seeds/LeadsSeeder.php location.

Now we will create model and migration by using this command.

php artisan make:model Lead -m

Now you will add this code in Leads migration at /database/migrations location.

<?php

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

class CreateLeadsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('leads', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->timestamps();
        });
    }

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

and this is the code in Lead model.

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Lead extends Model {
	protected $fillable = [ 'name', 'email' ];
}

Now add this code in LeadSeeder.php.

<?php

use Illuminate\Database\Seeder;

class LeadsSeeder extends Seeder {
	/**
	 * Run the database seeds.
	 *
	 * @return void
	 */
	public function run() {

		$now = \Illuminate\Support\Carbon::now('utc')->toDateTimeString();

		$leads = [
			[
				'name'  => 'Ryan',
				'email' => '[email protected]',
				'created_at'=> $now,
				'updated_at'=> $now
			],
			[
				'name'  => 'Emily',
				'email' => '[email protected]',
				'created_at'=> $now,
				'updated_at'=> $now
			]
		];

		\App\Lead::insert( $leads );
	}
}

Now we need to run this command so that our Laravel Installation got to recognise LeadsSeeder.

composer update -o

Then we will run migrations to make sure leads table is created in our database.

php artisan migrate

Now we will run this command to Seed values in our database.

php artisan db:seed --class=LeadsSeeder

Now we can add this to our Database seeder class and this way whenever seeder will run it will run LeadsSeeder, automatically. So we should add this code in DatabaseSeeder class.

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
         $this->call(LeadsSeeder::class);
    }
}

And now our LeadsSeeder will add values in our Database by using simply this command.

php artisan db:seed

I have now you have good understanding of Laravel seeders and the concept of Seeding overall. If you have any questions please leave a comment. We really appreciate that.