如何在Input :: all()中获取数组数据并将其作为hasMany()关系存储到实际记录中?

如何在Input :: all()中获取数组数据并将其作为hasMany()关系存储到实际记录中?

问题描述:

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?

我正在处理一个项目并且有一个表诊断。 还有另一个表diagnostic_detail。 诊断与diagnostic_detail具有一对多的关系。 进入诊断控制器以创建新记录的后期数据包含有关诊断本身的所有信息以及要推送到diagnostic_detail方法的一系列详细信息。 p>

现在很简单 创建诊断记录: p>

  Diagnostic :: create(Input :: all()); 
  code>  pre> 
 
 

但是 如何获取详细信息的所有数据并创建它? p> div>

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