Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

christiant's avatar

How to tell the Controller that form fields belong hasMany Relationship of t he Model

Hi there, so I have a model Flight, that has to airport-Information airport_depature_id and airport_destination_id that reference with a belongsTo to the model Airport.

   public function AirportDeparture()
    {
        return $this->belongsTo(Airport::class, 'airport_departure_id', 'id');
    }

In my Controllers I can use this relationship to get attributes e.g. $Flight->AirportDestination->Name as expected.

However I wanted to add the possibility, where a flight could have multiple sections e.g. New York, Chicago, Los Angeles, Sydney. So I created another model FlightSection (Migration contains: flight_id (Primary Key), section_id (PK), airport_id (FK))

In the Model Flight I added this Relationship:

    public function FlightSections()
    {
        return $this->hasMany(FlightSection::class, 'flight_id', 'id');
    }

And in the Model FlightSection I added this Relationship:

    public function FlightSections()
    {
        return $this->belongsTo(Airport::class, 'airport_id', 'id');
    }

I have a multi step form, that is collecting this information (and other) into the session and every step adds the data from the particular form step like this:

/* This is declared in the first PostController $flight = new Flight();  */

        $validatedData = request()->validate([
            'flight_has_more_sections' => ['required', 'boolean'],
            'sections' => ['required', 'array'],
            'sections.*.airport_id' => ['required', 'integer'],
        ]);

        $flight->fill($validatedData);

        $request->session()->put('flight', $flight);

In case the user would jump back in the form I want to re-populate the information back into the form via the session. However I do not know, where to put the hint for the Model that the 'sections'-array from the form belongs to the relationship 'FlightSections' and therefore I can use it again like $flight->FlightSections->Airport->Name in the form.

Hopefully I am just overlooking something basic rule here.

Cheers, Christian

0 likes
3 replies
christiant's avatar

I thought that I can only use old() when for example the validation has error and you will return to the form page. My szenario is, when the user uses the back-button in my form or jumps directly to this step-url.

Another aspect is, that later in the table of the FlighSection-Model I just want to save the airport_id and with the relationship access the additional attributes. As mentioned before the query of those attributes already works for belongsto-Attributes in the FlightModel, so I hope I can get the same behavior for the hasMany-Relationships

christiant's avatar

so I tried another approch and it works for what I want to do, but I am not sure if it is the "right" way to do it: I splitted the validation in two parts and then created for each entry of the array a new FlightSections and inserted into the Model Flight via the refence $flight->FlightSections[]

/* This is declared in the first PostController $flight = new Flight();  */

        $validatedData = request()->validate([
            'flight_has_more_sections' => ['required', 'boolean'],
        ]);

        $flight_case->fill($validatedData);

        $validatedData = request()->validate([
            'sections' => ['exclude_if:flight_has_more_sections,false', 'required', 'array'],
            'sections.*.airport_id' => ['exclude_if:flight_has_more_sections,false', 'required', 'integer'],
        ]);

        foreach ($validatedData["sections"] as $key => $airports) {
            foreach ($airports as $airport) {
                $flight_section = new FlightSection();
                $flight_section->section_id = $key;
                $flight_section->airport_id = $airport;
                $flight->FlightSections[$key] = $flight_case_section;
            }
        }

        $request->session()->put('flight', $flight);

in the blade I now can use this:

/* $i from my for loop in the blade */
$flight->FlightSections[$i]->Airport

Please or to participate in this conversation.