I’m doing it this way:
creating seeder with artisan:
php artisan make:seeder UsersTableSeeder
then you open the file and you enter users:
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('users')->insert([
'name' => 'User1',
'email' => 'user1@email.com',
'password' => bcrypt('password'),
]);
DB::table('users')->insert([
'name' => 'user2',
'email' => 'user2@email.com',
'password' => bcrypt('password'),
]);
}
}
If you want to generate random list of users, you can use factories:
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\User::class, 50)->create();
/* or you can add also another table that is dependent on user_id:*/
/*factory(App\User::class, 50)->create()->each(function($u) {
$userId = $u->id;
DB::table('posts')->insert([
'body' => str_random(100),
'user_id' => $userId,
]);
});*/
}
}
Then in the file app/database/seeds/DatabaseSeeder.php uncomment or add in run function a line:
$this->call(UsersTableSeeder::class);
it will look like this:
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(UsersTableSeeder::class);
}
}
At the end you run:
php artisan db:seed
or
php artisan db:seed --class=UsersTableSeeder
I hope will help someone.
PS: this was done on Laravel 5.3