Edit File: UserTableSeeder.php
<?php namespace Database\Seeders; use App\Enums\GenderEnum; use App\Models\City; use App\Models\Country; use App\Models\User; use Faker\Factory as Faker; use Illuminate\Database\Seeder; use Illuminate\Support\Facades\DB; class UserTableSeeder extends Seeder { public function run() { $faker = Faker::create('ar_SA'); $users = []; $batchSize = 50; // Set the batch size for insert operation // Fetch all country and city IDs once to avoid repeated queries inside the loop $countryIds = Country::pluck('id')->toArray(); $cityIds = City::pluck('id')->toArray(); for ($i = 0; $i < 200; $i++) { $users[] = [ 'image' => 'users/default.png', 'first_name' => $faker->firstName, 'last_name' => $faker->lastName, 'phone' => "51111111$i", 'country_code' => '+966', 'email' => $faker->unique()->safeEmail, 'country_id' => $faker->randomElement($countryIds), 'city_id' => $faker->randomElement($cityIds), 'bio' => $faker->words(10, true), 'gender' => $faker->randomElement(GenderEnum::values()), 'birth_date' => $faker->date(), 'is_notify' => rand(0, 1), 'is_blocked' => rand(0, 1), 'is_active' => rand(0, 1), 'lat' => $faker->latitude, 'lng' => $faker->longitude, 'map_desc' => $faker->address, 'created_at' => now(), 'updated_at' => now(), ]; if (count($users) === $batchSize) { // Insert users in batch DB::table('users')->insert($users); // Retrieve the newly inserted users' IDs $userIds = DB::table('users')->latest('id')->take($batchSize)->pluck('id'); // Create wallets for each user $this->createWallets($userIds); $users = []; } } // Insert remaining users and their wallets if any left if (!empty($users)) { DB::table('users')->insert($users); // Retrieve the remaining inserted users' IDs $userIds = DB::table('users')->latest('id')->take(count($users))->pluck('id'); // Create wallets for these users $this->createWallets($userIds); } } /** * Create a wallet for each user. * * @param array $userIds */ private function createWallets($userIds) { $wallets = []; foreach ($userIds as $userId) { $wallets[] = [ 'walletable_id' => $userId, 'walletable_type' => User::class, 'balance' => rand(0, 10000), // Random or default balance 'available_balance' => rand(0, 10000), 'created_at' => now(), 'updated_at' => now(), ]; } // Insert wallets in batch DB::table('wallets')->insert($wallets); } }
Back to File Manager