[SOLVED] How to execute migration and seeding in Laravel

PHP artisan migrates artisan command use for migration, PHP artisan db:seed artisan command use for all data seeding. But, How to execute migration and seeding in Laravel ?

It’s very simple to do that. When you need to execute single seeding file like UserRoleSeeder.

File location in your application is like database/seeds/UserRoleSeeder.

Now the artisan command is PHP artisan db:seed –class=UserRoleSeeder

Run above artisan command and it will seed only UserRoleSeeder class from your DatabaseSeeder file

( DatabaseSeeder file located at database/seeds/DatabaseSeeder )

In your DatabaseSeeder file,


Code :

use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;

class DatabaseSeeder extends Seeder
    public function run()

And here is UserRoleSeeder file.


Run method code :

use Illuminate\Database\Seeder;
use Wooter\UserRole;

class UserRoleSeeder extends Seeder
    public function run()
        $bdHints = factory(User::class)->create([
            'first_name' => 'BDF',
            'last_name' => 'FinalHints',
            'email' => 'bd@finalhints.com',
            'password' => bcrypt('BD-Finalhints'),
            'verified' => 1]

        $userRole = new UserRole;
        $userRole->user_id = $bdHints->id;
        $userRole->role_id = Role::ORGANIZATION;

A purpose of inserting large amount records, we can use model factories. It generates large amounts of database records. Review model factories documentation to learn how to define your factories. After defining your factories, you can use factory helper function to insert records into your database. Laravel allows you to define a default set of attributes for each of your Eloquent models using model factories.

To get started, use database/factories/ModelFactory.php file in application.


ModelFactory.php file containing all factories definition. Here, showing one factory definition.

$factory->define(App\User::class, function (Faker\Generator $faker) {
    static $password;

    return [
        'name' => $faker->name,
        'email' => $faker->unique()->safeEmail,
        'password' => $password ?: $password = bcrypt('secret'),
        'remember_token' => str_random(10),

However, you stuck while executing specific migration and seeding in Laravel let me know. Always ready to help you.


Leave a Reply

Your email address will not be published. Required fields are marked *