@gacho create new CitiesTableSeeder add your query there and call it as the first seeder in DatabaseSeeder.
public function run()
{
$this->call(CitiesTableSeeder::class);
// other seeders
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Now the only way for my site to work properly is to do things in this order. First I type php artisan migrate:fresh command, then I manually add that query (by copying from script) in sql tab in phpmyadmin, and then I type php artisan db:seed command. Now, is there a way for me to automatically populate cities table without having to manually add that query. So that when I enter php artisan migrate:fresh --seed command it would first erase all data then populate cities table from .sql file and then finish seeding other tables? Any help is appreciated. Here is my seeder currently.
UserSeeder.php
<?php
use App\City;
use App\User;
use App\Gender;
use Carbon\Carbon;
use Faker\Factory as Faker;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\Hash;
/**
* Class UserSeeder
*/
class UserSeeder extends Seeder
{
use ChunkSeeder;
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$genders = DB::table('genders')->insert([
[
'genders' => 'Woman',
],
[
'genders' => 'Woman Looking For Woman',
],
[
'genders' => 'Man',
]
]);
$genderIds = Gender::pluck('id');
$cityIds = City::pluck('id');
$seed = [];
for ($i = 0, $n = 150; $i < $n; $i++) {
$seed[] = factory(User::class)->make(
[
'gender_id' => $genderIds->random(),
'city_id' => $cityIds->random(),
'email_verified_at' => Carbon::now(),
'premium_purchased_at' => Carbon::now(),
'mobile_verified_at' => Carbon::now(),
'mobile_verification_id' => rand(0, 20),
]
)->toArray();
}
$this->seedChunks($seed, [User::class, 'insert']);
}
}
Please or to participate in this conversation.