List of columns
###
###
######
Laravel has seeder feature, To insert some master data in database while migrating.
php artisan make:seeder UserSeeder
<?php
namespace Database\Seeders;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Hash;
use Carbon\Carbon;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$users = [
[
'user_id' => '4bace869-9bc0-424c-9292-111dac5b7ee7',
'first_name' => 'Admin',
'middle_name' => 'D',
'last_name' => 'Doe',
'email' => 'admin@mailinator.com',
'password' => Hash::make('India@123'),
'role_id' => 1,
'created_at' => now(),
'updated_at' => now(),
],
[
'user_id' => Str::uuid(),
'first_name' => 'Jane',
'middle_name' => 'E',
'last_name' => 'Smith',
'email' => 'sid@mailinator.com',
'password' => Hash::make('India@123'),
'role_id' => 2,
'created_at' => now(),
'updated_at' => now(),
],
[
'user_id' => Str::uuid(),
'first_name' => 'Alice',
'middle_name' => 'F',
'last_name' => 'Johnson',
'email' => 'alice.johnson@example.com',
'password' => Hash::make('password'),
'role_id' => 1,
'created_at' => now(),
'updated_at' => now(),
],
// Add more users as needed
];
// Insert users and create profiles
foreach ($users as $user) {
DB::table('users')->insert($user);
$profileData = [
'user_id' => $user['user_id'],
'kyc_status' => 'Pending',
'dob' => Carbon::now()->format('Y-m-d'), // Adjust as needed
'account_type' => $user['role_id'] === 1 ? 'Corporate' : ($user['role_id'] === 2 ? 'Personal' : 'Personal'),
'sex' => 'Male', // Adjust as needed
'phone_no' => '1234567890', // Adjust as needed
'country_id' => 1, // Adjust as needed
];
if ($profileData['account_type'] === 'Corporate') {
$profileData['company'] = 'Example Corp';
$profileData['company_sector'] = 'Technology';
$profileData['company_address'] = '1234 Example St';
}
DB::table('profiles')->insert($profileData);
}
}
}
After this you need to update userSeeder on databaseSeeder.php
and then finally run migrate
<?php
namespace Database\Seeders;
use App\Models\User;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*/
public function run(): void
{
// User::factory(10)->create();
// User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
// Call the UserSeeder
$this->call(UserSeeder::class);
}
}
and then finally run migrate
php artisan migrate:fresh --seed
Comments
Post a Comment
Thank you :)