Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 对Laravel中的多个子记录使用createOrUpdate_Php_Laravel_Laravel 5_Relationship - Fatal编程技术网

Php 对Laravel中的多个子记录使用createOrUpdate

Php 对Laravel中的多个子记录使用createOrUpdate,php,laravel,laravel-5,relationship,Php,Laravel,Laravel 5,Relationship,我有一个“装运”模型,其中有许多“装运详细信息”模型记录 public function shipment_details(){ return $this->hasMany('App\Shipment_Detail', 'shipmentID','id'); } 当我创建“装运”记录时,最初创建“装运详细信息”没有问题 当我想要更新“装运”记录,并且最终需要在下面添加额外的“装运详细信息”记录时,我的问题就出现了 目前,我的“装运详细信息”html来自刀片服务器: <t

我有一个“装运”模型,其中有许多“装运详细信息”模型记录

public function shipment_details(){
      return $this->hasMany('App\Shipment_Detail', 'shipmentID','id');
}
当我创建“装运”记录时,最初创建“装运详细信息”没有问题

当我想要更新“装运”记录,并且最终需要在下面添加额外的“装运详细信息”记录时,我的问题就出现了

目前,我的“装运详细信息”html来自刀片服务器:

<tbody>
    @foreach($shipment_details as $sd)
    <tr style="height:40px">
        <td style="width:8%;text-align:center;">{{Form::text('shipment_details['.$sd->id.'][piecesNumber]', $sd->pieces_number, array('class' => 'form-control','placeholder'=>'No. Pieces','required','id'=>'piecesNumber'))}}
        </td>
        <td style="width:16%;text-align:center;">
            {!! Form::select('shipment_details['.$sd->id.'][piecesType]', $piecetypes, $sd->pieces_type, ['id' => 'pieces_type', 'class' => 'form-control full-width','required']) !!}    
        </td>
        <td>
            {!! Form::select('shipment_details['.$sd->id.'][rateType]', $ratetypes, $sd->rate_type, ['id' => 'rateType', 'class' => 'form-control full-width','required']) !!}
        </td>
        <td style="width:16.5%;text-align:center;">
            {{Form::text('shipment_details['.$sd->id.'][weight]', $sd->weight, array('class' => 'form-control','placeholder'=>'Weight','required','id'=>'weight'))}}    
        </td>
        <td style="width:16.5%;text-align:center;">
            {{Form::select('shipment_details['.$sd->id.'][hazmat]',array(
                                    '0'=>'No',
                                    '1'=>'Yes',
                                ), $sd->hazmat, array('class' => 'form-control','id'=>'hazmat'))}}   
        </td>
        <td style="width:16.5%;text-align:center;">
                            {{Form::text('shipment_details['.$sd->id.'][description]', $sd->description, array('class' => 'form-control','placeholder'=>'Description','required','id'=>'description'))}} 
        </td>
        <td style="width:16.5%;text-align:center;">
            {{Form::text('shipment_details['.$sd->id.'][charge]', $sd->charge, array('class' => 'form-control','placeholder'=>'Charge','required','id'=>'charge'))}} 
        </td>
        <td><button type="button" name="add" id="add" class="btn btn-success">Add More</button></td>
    </tr>

    @endforeach
</tbody>
所以我的问题是,有没有办法获得更多的“装运详细信息”?它需要使用像updateOrCreate这样的东西,还是需要其他东西

更新 这就是我的数组目前发布的方式,并且当前正在使用错误-未定义的索引:piecesType发布。目前,如果我更新了当前装运的详细信息,它可以正常工作,它添加了一个新的,这就产生了问题

shipment_details    

array:8 [▼
  13149 => array:7 [▼
    "piecesNumber" => "1"
    "piecesType" => "1"
    "rateType" => "1"
    "weight" => "12"
    "hazmat" => "0"
    "description" => "124"
    "charge" => "12.00"
  ]
  "piecesNumber" => array:1 [▼
    0 => "3"
  ]
  "piecesType" => array:1 [▼
    0 => "2"
  ]
  "rateType" => array:1 [▼
    0 => "2"
  ]
  "weight" => array:1 [▼
    0 => "1200"
  ]
  "hazmat" => array:1 [▼
    0 => "Yes"
  ]
  "description" => array:1 [▼
    0 => "desc2"
  ]
  "charge" => array:1 [▼
    0 => "40.00"
  ]
]
更新 ALEXEY的货运控制员

foreach ($request->shipment_details as $id => $details) {
            $shipdetail = Shipment_Detail::updateOrCreate(['id' => $id], [
            'pieces_type' => $details['piecesType'][0],
            'pieces_number' => $details['piecesNumber'][0],
            'rate_type' => $details['rateType'][0],
            'weight' => $details['weight'][0],
            'charge' => $details['charge'][0],
            'description' => $details['description'][0],
            'hazmat' => $details['hazmat'][0]
                ]);
        }
更新 更新了dd($id,$details)


但它不包括我试图通过此保存添加的最近添加的行。

您可以像这样使用
updateOrCreate()

foreach ($request->shipment_details as $id => $details) {
    $shipdetail = Shipment_Detail::updateOrCreate(['id' => $id], [
        'pieces_type' => $details['piecesType'],
        'pieces_number' => $details['piecesNumber'],
        'rate_type' => $details['rateType'],
        'weight' => $details['weight'],
        'charge' => $details['charge'],
        'description' => $details['description'],
        'hazmat' => $details['hazmat']
    ]);
}

您所说的“是否有办法获得其他装运详细信息”是什么意思?比如说,当我最初创建装运记录时,我在其中添加了1个装运详细信息,但如果我返回到装运并希望在编辑装运时添加另一个装运详细信息,我会单击“添加”按钮并为另一个“装运详细信息”记录添加其他信息。当我在装运上单击“保存”时,它会通过,看到第一批装运的详细信息有一个现有记录,必要时会进行更新,然后转到第二批装运的详细信息,在该详细信息中,它会识别没有当前记录并将其添加。就像处理发票模型一样,您想返回并向特定模型添加更多发票项。那么,是的,您可以使用
updateOrCreate
方法更新现有模型,并在必要时创建新模型。我出现了一个错误,称为“未定义索引”,我相信这就是我的数组的发布方式,所以我将在上面发布。@Matthew它与问题无关,这意味着数组中缺少一些数据。你可以通过
dd($details)
查看,那么我应该发布一个新问题吗?因为所有这些看起来都像是新行没有在单个数组行中发布。@Matthew哦,我可以看出您已经更新了问题。问题是因为您有嵌套数组,所以如果要获取
desc2
,请执行此
$details['description'][0]
而不是
$details['description']
。如果要从第一个子数组获取数据,请执行以下操作:
$details=array\u first($details)然后像您一样处理数据。好的,我会的,谢谢您的帮助。我会接受你的回答。再次感谢!
foreach ($request->shipment_details as $id => $details) {
            $shipdetail = Shipment_Detail::updateOrCreate(['id' => $id], [
            'pieces_type' => $details['piecesType'][0],
            'pieces_number' => $details['piecesNumber'][0],
            'rate_type' => $details['rateType'][0],
            'weight' => $details['weight'][0],
            'charge' => $details['charge'][0],
            'description' => $details['description'][0],
            'hazmat' => $details['hazmat'][0]
                ]);
        }
13149

array:7 [▼
  "piecesNumber" => "1"
  "piecesType" => "1"
  "rateType" => "1"
  "weight" => "12"
  "hazmat" => "0"
  "description" => "124"
  "charge" => "12.00"
]
foreach ($request->shipment_details as $id => $details) {
    $shipdetail = Shipment_Detail::updateOrCreate(['id' => $id], [
        'pieces_type' => $details['piecesType'],
        'pieces_number' => $details['piecesNumber'],
        'rate_type' => $details['rateType'],
        'weight' => $details['weight'],
        'charge' => $details['charge'],
        'description' => $details['description'],
        'hazmat' => $details['hazmat']
    ]);
}