Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views : Ad Clicks : Ad Views :
InvestmentNovel

Latest Programming Tutorial Blog

Laravel Database Seeder Tutorial Example

/
/
/
106 Views

Laravel Database Seeder Tutorial Example is today’s leading topic. Laravel includes an easy method of seeding your database with test data using seed classes. All seed classes are stored under the database/seeds directory.  Seed classes may have some name you wish but probably should follow any sensible convention, such as UsersTableSeeder, ItemsTableSeeder etc.  Default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes, letting you control the seeding order. If you require more detail, then move to Laravel Documentation.

Laravel Database Seeder Tutorial Example

We are going to Configure Laravel Project.

#1: Install Laravel Project

Install Laravel 5.6 Project by the typing following command.

composer create-project --prefer-dist laravel/laravel laravelseeder

#2: Configure SQL Database

Now we can setup database credentials.

//.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravelseeder
DB_USERNAME=root
DB_PASSWORD=

#3: Create Migration File

Hit following command in cmd to create a migration file.

php artisan make:migration create_items_table

It will create a create_items_table.php  migration file.

// create_items_table

public function up()
    {
        Schema::create('items', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->string('name');
            $table->timestamps();
        });
    }

Now we also create another migration file so you can type the following command in cmd.

php artisan make:migration create_orders_table

It will create a create_orders_table.php  migration file.

// create_orders_table

public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id');
            $table->integer('item_id');
            $table->timestamps();
        });
    }

To run all of your leading migrations, execute a migrate Artisan command:

php artisan migrate

It will build five tables in your database.

  1. users
  2. password_resets
  3. items
  4. orders
  5. migrations

#4: Generate Seeders

To create a seeder, execute the make: seeder artisan command. All seeders created by the framework will be placed in the database/seeds directory:

Here we create three seeders because we have three table so we can create seeders one by one.

php artisan make:seeder UsersTableSeeder

It will create the UsersTableSeeder.php file. As an example, let’s modify the UserTableSeeder class and add a database insert statement to the run method:

//UsersTableSeeder.php

<?php

use Illuminate\Database\Seeder;

class UsersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('users')->insert([
            'name' => 'Smith',
            'email' => 'smith@gmail.com',
            'password' => bcrypt('secret'),
        ]);
        DB::table('users')->insert([
            'name' => 'Messi',
            'email' => 'messi@gmail.com',
            'password' => bcrypt('secret'),
        ]);
        DB::table('users')->insert([
            'name' => 'Ronaldo',
            'email' => 'ronaldo@gmail.com',
            'password' => bcrypt('secret'),
        ]);
        DB::table('users')->insert([
            'name' => 'Neymar',
            'email' => 'neymar@gmail.com',
            'password' => bcrypt('secret'),
        ]);
        DB::table('users')->insert([
            'name' => 'Bale',
            'email' => 'bale@gmail.com',
            'password' => bcrypt('secret'),
        ]);
    }
}
php artisan make:seeder ItemsTableSeeder

It will create the ItemsTableSeeder.php file.  As an example, let’s modify the ItemsTableSeeder class and add a database insert statement to the run method:

//ItemsTableSeeder.php

<?php

use Illuminate\Database\Seeder;

class ItemsTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('items')->insert([
            'name' => 'Stationery',
            'user_id' => '3',
        ]);
        DB::table('items')->insert([
            'name' => 'Furniture',
            'user_id' => '5',
        ]);
        DB::table('items')->insert([
            'name' => 'Macbook',
            'user_id' => '1',
        ]);
        DB::table('items')->insert([
            'name' => 'Camera',
            'user_id' => '4',
        ]);
        DB::table('items')->insert([
            'name' => 'Sofa',
            'user_id' => '2',
        ]);
    }
}
php artisan make:seeder OrdersTableSeeder

It will create the OrdersTableSeeder.php file.  As an example, let’s modify the OrdersTableSeeder class and add a database insert statement to the run method:

//OrdersTableSeeder.php

<?php

use Illuminate\Database\Seeder;

class OrdersTableSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        DB::table('orders')->insert([
            'user_id' => '3',
            'item_id' => '4',
        ]);
        DB::table('orders')->insert([
            'user_id' => '5',
            'item_id' => '2',
        ]);
        DB::table('orders')->insert([
            'user_id' => '1',
            'item_id' => '3',
        ]);
        DB::table('orders')->insert([
            'user_id' => '4',
            'item_id' => '4',
        ]);
        DB::table('orders')->insert([
            'user_id' => '2',
            'item_id' => '5',
        ]);

    }
}

#5: Calling Seeders

Within a DatabaseSeeder class, you may use the call method to execute additional seed classes. Using a call method allows you to break up your database seeding into multiple files so that no single seeder class becomes overwhelmingly large. Pass the name of a seeder class you wish to run:

//DatabaseSeeder.php

<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;


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

#6: Running Seeders

You may want to regenerate Composer’s autoloader using the following command:

composer dump-autoload

Now you may use the following artisan command to seed your database.

php artisan db:seed

By default, the db: seed command runs a  DatabaseSeeder class, which may be used to call other seed classes.

Now you can see below screenshot of three table.

  1. UsersTable:

Laravel Database Seeder Tutorial

2. ItemsTable:

Laravel Database Seeder Example

3. OrdersTable:

Laravel 5.6 Database Seeder Tutorial Example

Finally, Our Laravel Database Seeder Tutorial Example is over. Thanks for taking.

  • Facebook
  • Twitter
  • Google+
  • Linkedin
  • Pinterest

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This div height required for enabling the sticky sidebar