It sounds like you just need to populate the previous values. This is documented here: https://laravel.com/docs/master/requests#retrieving-old-input
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
Please or to participate in this conversation.