I am working on a project and there is a table diagnostic. There is another table diagnostic_detail. diagnostic has a one to many relation with diagnostic_detail. The post data coming in the Diagnostic controller to make a new record has all the information about the diagnostic itself and an array of details to be pushed into the diagnostic_detail method.
Now it is simple to create the diagnostic record:
Diagnostic::create(Input::all());
But how do I take all the data for the detail and create it?
You'll have to loop and create the detail object manually. Try this:
$details = array();
foreach(Input::get('diagnostic_detail') as $detail){
$details[] = new DiagnosticDetail($detail);
}
$diagnostic->details()->saveMany($details);
In this example, DiagnosticDetail
is the model that is referenced by hasMany()
and details
is the hasMany relationship.
More information on inserting related models