Is there a way to seed a table in Laravel for each record seeded in another table and using columns of each record? (Dependent seeding)

This is more like model event listener creating some number of other records connected to that particular record creating and using a foreign key. However I want this to occur during seeding. Let’s say I have a Flight model/table, I want each time a create a record either through manually or seeding, it automatically creates the seats (On the Seat model or table) which users can book. Also the number of seats created for that flight depends on a column on the flight called “available_seats”. I have already implemented this using model event listeners but it doesn’t seem to work when I seed my flights. Now because seats are usually large in number, I may need ways to optimize the seats creation time both manually and seeding

This is my Flight Seeder file:

<?php

namespace DatabaseSeeders;

use AppModelsSeat;
use AppModelsFlight;
use IlluminateDatabaseSeeder;
use IlluminateDatabaseConsoleSeedsWithoutModelEvents;

class FlightSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Creating large record of flights using factories
        $flights = Flight::factory(20)->create();

        foreach ($flights as $flight) {
            $this->createSeats($flight);
        }
    }

    
    // Function to create flight seats
    public function createSeats(Flight $flight)
    {
        // Define the seat classes and the number of seats in each class
        $seatClasses = [
            'economy' => floor(0.70 * $flight->available_seat),
            'premium economy' => floor(0.10 * $flight->available_seat),
            'business' => floor(0.15 * $flight->available_seat),
            'first class' => floor(0.05 * $flight->available_seat),
        ];

        foreach ($seatClasses as $class => $count) {
            // Determining seat price based on price
            if ($class == 'economy') {
                $price = $flight->economy_price;
            } else if ($class == 'premium economy') {
                $price = $flight->premium_economy_price;
            } else if ($class == 'business') {
                $price = $flight->business_price;
            } else {
                $price = $flight->first_class_price;
            }

            // Loop to create class seats
            for ($i = 1; $i <= $count; $i++) {
                Seat::create([
                    'flight_id' => $flight->id,
                    'seat_number' => strtoupper(substr($class, 0, 1)) . $i, // Example: E1, E2, ..., B1, B2, ...
                    'seat_class' => $class,
                    'price' => $price,
                    'status' => 'available'
                ]);
            }
        }
    }
}

It only seeds the flight table but doesn’t create the seats as expected. I’m I missing something or are there ways to connect it to my model event listener? Here is my flight factory code if required:

<?php

namespace DatabaseFactories;

use AppCustomFunctions;
use CarbonCarbon;
use IlluminateDatabaseEloquentFactoriesFactory;

/**
 * @extends IlluminateDatabaseEloquentFactoriesFactory<AppModelsFlight>
 */
class FlightFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        // Date extension by days
        $dateAddition = rand(0, 7);

        // Base flight price
        $base_price = fake()->randomFloat(2, 1000, 5000);

        return [
            'flight_id' => Functions::generateFlightID(),
            'airline_id' => rand(1, 5), // Random airline ID between 1 and 10
            'origin_id' => rand(1, 10), // Random origin ID between 1 and 10
            'destination_id' => rand(1, 10), // Random destination ID between 1 and 10
            'departure_time' => Carbon::now()->addDays($dateAddition)->format('Y-m-d H:i:s'), // Departure at least today or in the next 7 days
            'arrival_time' => fake()->dateTimeBetween(Carbon::now()->addDays($dateAddition), Carbon::now()->addDays($dateAddition + 3)), // Arrival sometime between departure date and 3 days after
            'economy_price' => $base_price, // Price between 100.00 and 1000.00
            'premium_economy_price' => $base_price + 500, // Price dependent on the economy/base price
            'business_price' => $base_price + 1000, // Price dependent on the economy/base price
            'first_class_price' => $base_price + 3000, // Price dependent on the economy/base price
            'available_seats' => rand(150, 300)
        ];
    }
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật