xxxxxxxxxx
Database seeder is used to populate tables with data.
Model factories is a convenient centralized place to define how your models should be populated with fake data.
In seeder class you would leverage model factories, and model factories will most likely use another library to generate random fake data, such as fzaninotto/faker.
xxxxxxxxxx
//All Seeders
php artisan db:seed
//One Seeder
php artisan db:seed --class=NameSeeder
xxxxxxxxxx
CREATE SEEDER -> run php artisan command:
php artisan make:seeder UsersTableSeeder
xxxxxxxxxx
step-1- php artisan make:seeder yourSeedername
step-2- //add data in inside run function in your new created seeder. eg
$Records = [
['id'=>1, 'name'=>'abc','email'=>'abc@gmail.com'],
['id'=>2, 'name'=>'xyz','email'=>'xyz@gmail.com']
];
YourModel::insert($Records);
//don't forget to use model in top of your seeder
step-3- //register seeder in run function inside Database/Seeders/DatabaseSeers.php as follows
$this->call(yourseeder::class);
step-4- //Now run following command
php artisan db:seed
xxxxxxxxxx
<?php
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => Str::random(10),
'email' => Str::random(10).'@gmail.com',
'password' => Hash::make('password'),
]);
}
}