Database seeding

Laravel includes a simple method of seeding your database with test data using seed classes. All seed classes are stored in the database/seeds directory. Seed classes may have any name you wish.

By default, a DatabaseSeeder class is defined for you. From this class, you may use the call method to run other seed classes

Create Seeders

To generate a seeder, execute the make:seeder Artisan command. All seeders generated by the framework will be placed in the database/seeds directory

php artisan make:seeder UserSeeder

php artisan make:seeder UsersTableSeeder

// here UsersTableSeeder is the class name of seeder class.

After that seeder class create UserSeeder class in database/seeds directory.

<?php

use Illuminate\Database\Seeder;

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

insert table data in run method.

A seeder class only contains one method by default: run. This method is called when the db:seed Artisan command is executed. Within the run method, you may insert data into your database however you wish. You may use the query builder to manually insert data or you may use Eloquent model factories.

<?php

use Illuminate\Database\Seeder;

class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => 'mahtab',
'email' => 'mahtab@email.com',
'password' => bcrypt('mahtab'),
//'password' => Hash::make('mahtab'), //'remember_token' => str_random(10), ]);
DB::table('users')->insert([
'name' => 'nusrat',
'email' => 'nusrat@email.com',
'password' => bcrypt('nusrat'),
//'password' => Hash::make('nusrat'), //'remember_token' => str_random(10), ]);
}



public function run()
{
User::create([
'name' => 'John Smith',
'email' => 'john_smith@gmail.com',
'password' => Hash::make('password'),
'remember_token' => str_random(10),
]);
} }

public function run()
{
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => Hash::make('password'),
]);
}

public function run()
{
for ($i=0; $i < 3; $i++) {
User::create([
'name' => str_random(8),
'email' => str_random(12).'@mail.com',
'password' => bcrypt('123456')
]);
}
}

DatabaseSeeder class

<?php

use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call(UserSeeder::class);
$this->command->info('User table seeded!');

//$this->call(CountriesTableSeeder::class);
}
}

Running Seeders

Once you have written your seeder, you may need to regenerate Composer's autoloader using the dump-autoload command

composer dump-autoload

Now you may use the db:seed Artisan command to seed your database. By default, the db:seed command runs the DatabaseSeeder class, which may be used to call other seed classes. However, you may use the --class option to specify a specific seeder class to run individually:

php artisan db:seed

Seeding: UsersTableSeeder
Seeded: UsersTableSeeder (0.29 seconds)
User table seeded!
Database seeding completed successfully.

Or

php artisan db:seed --class=UserSeeder

You may also seed your database using the migrate:fresh command, which will drop all tables and re-run all of your migrations. This command is useful for completely re-building your database

php artisan migrate:fresh --seed

Forcing Seeders To Run In Production

Some seeding operations may cause you to alter or lose data. In order to protect you from running seeding commands against your production database, you will be prompted for confirmation before the seeders are executed. To force the seeders to run without a prompt, use the --force flag

php artisan db:seed --force





Previous Next


Trending Tutorials




Review & Rating

0.0 / 5

0 Review

5
(0)

4
(0)

3
(0)

2
(0)

1
(0)

Write Review Here


Ittutorial