#All of them
php artisan db:seed
#One class
php artisan db:seed --class=UserSeeder
1#create a laravel seeding file
php artisan make:seeder <seeder file name>
#example
php artisan make:seeder ShopSeeder
2#after seeding file create and seed data added,run this command below to add thos data in database
php artisan db:seed --class=<seeder file name>
#example
php artisan db:seed --class=ShopSeeder
3# if want to add all seeder fill data just run
php artisan db:seed
php artisan make:seeder NameOfTheSeeder
php artisan migrate:fresh --seed
//All Seeders
php artisan db:seed
//One Seeder
php artisan db:seed --class=NameSeeder
CREATE SEEDER -> run php artisan command:
php artisan make:seeder UsersTableSeeder
php artisan make:seeder UsersTableSeeder
php artisan make:seeder MoviesTableSeeder
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
#To create a seeder
php artisan make:seeder CategorySeeder
use IlluminateSupportFacadesDB;
use IlluminateSupportFacadesHash;
...
...
DB::table('users')->insert([
'name' => 'John Doe',
'email' => 'john@doe.com',
'password' => Hash::make('password')
]);
php artisan make:seeder ArticlesTableSeeder
$ php artisan make:seeder MoviesTableSeeder
let's see simple example:
you can use following command to all seeders in laravel application:
***************************
php artisan db:seed
***************************
you have to register all seeder in DatabaseSeeder.php file and that will run all seeders at a time, register as like bellow:
database/seeders/DatabaseSeeder.php
<?php
namespace DatabaseSeeders;
use IlluminateDatabaseSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
UserSeeder::class
AdminSeeder::class
]);
}
}
<?php
use IlluminateDatabaseSeeder;
use IlluminateSupportFacadesDB;
use IlluminateSupportFacadesHash;
use IlluminateSupportStr;
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'),
]);
}
}
// in DatabaseSeeder.php file
$this->call([
UserSeeder::class,
]);
php artisan db:seed
php artisan db:seed --class=UserSeeder
make DatabaseSeerder class and call function with seeder Array
<?php
namespace DatabaseSeeders;
use IlluminateDatabaseSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$this->call([
UserSeeder::class
AdminSeeder::class
]);
}
}
/**
* Run the database seeders.
*
* @return void
*/
public function run()
{
$this->call([
UserSeeder::class,
PostSeeder::class,
CommentSeeder::class,
]);
}
php artisan db:seed --force
$ php artisan db:seed
namespace DatabaseSeeders;
use AppModelsCategory;
use AppModelsPost;
use AppModelsUser;
use IlluminateDatabaseSeeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
$users = User::factory(2)->create();
$categories = Category::factory(3)->create();
Post::factory(3)->create([
'user_id' => $users[0]->id,
'category_id' => $categories[0]->id
]);
Post::factory(3)->create([
'user_id' => $users[1]->id,
'category_id' => $categories[2]->id
]);
}
}
class DatabaseSeeder extends Seeder
{
public function run()
{
$this->call(ArticlesTableSeeder::class);
$this->call(UsersTableSeeder::class);
}
}
class UsersTableSeeder extends Seeder
{
public function run()
{
$limit = env('SEEDER_LIMIT', 1);
echo $this->limit;
}
}
//In terminal run:
SEEDER_LIMIT=10 php artisan db:seed --class=UsersTableSeeder
composer require orangehill/iseed
php artisan iseed my_table