xxxxxxxxxx
//just create following route and visit it.
use Illuminate\Support\Facades\Artisan;
Route::get('/migrate-seed', function () {
Artisan::call('migrate', ["--force" => true ]);
Artisan::call('db:seed', [
'--force' => true
]);
return 'migrated and seeded the data';
});
xxxxxxxxxx
public function run()
{
$this->call(RegiserUserSeeder::class);
}
php artisan migrate:refresh --seed
//Or to run specific seeder
php artisan seeder RegiserUserSeeder
xxxxxxxxxx
php artisan make:model MODEL_PATH\MODEL_NAME -ms
-m, --migration Create a new migration file for the model.
-s, --seeder Create a new seeder file for the model.
xxxxxxxxxx
// 1. Create the database seeder file using the make:seeder command
php artisan make:seeder DatabaseSeeder
// 2. Open the newly created seeder file (located in database/seeds) and define your seeder logic
// For example, let's say you have a 'users' table and you want to seed it with some dummy data
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
// Generate 10 dummy users and insert them into the 'users' table
DB::table('users')->insert([
[
'name' => 'John Doe',
'email' => 'john@example.com',
'password' => bcrypt('secret'),
],
[
'name' => 'Jane Smith',
'email' => 'jane@example.com',
'password' => bcrypt('password'),
],
// add more dummy users as needed
]);
}
}
// 3. Once you have defined your seeder logic, you can run the seeder using the db:seed command
php artisan db:seed
// The DatabaseSeeder's run method will be called, and the dummy data will be inserted into the 'users' table.