问题描述
尝试在控制器中访问类型为null的值的数组偏移量
foreach ($request->product_item_list as $xx)
echo $request->hidden_name_[$xx] . ' ' . $request->product_unit_price_[$xx] . ' ' . $request
->quantity_list_[$xx] . ' ' . $request->hidden_barcode_[$xx];
其中dd($ request-> product_item_list)喜欢
array:1 [▼
0 => "13"
]
dd($ request-> all())看起来
array:14 [▼
"_token" => "JuFDWGzK10dV00cwMaqgX4I9R7tbVLErJ11vxjYv"
"category_salesCoffee" => "6"
"hidden_name_12" => "alto"
"quantity_list_12" => null
"hidden_barcode_12" => "11120"
"product_unit_price_12" => null
"hidden_cost_12" => "14"
"product_item_list" => array:1 [▼
0 => "13"
]
"hidden_name_13" => "black"
"quantity_list_13" => "1"
"hidden_barcode_13" => "11130"
"product_unit_price_13" => "33"
"hidden_cost_13" => "14"
"product_count" => "2"
]
blade.PHP
@foreach ($products as $item)
<tr>
<td scope="row">{{ $item->id }}</td>
<td>
<input type="checkBox" name="product_item_list[]" id="product_item_list[]" value="{{$item->id}}" {{$item->quantity == 0 ? 'disabled' : ''}}>
</td>
<td id="product_name">{{$item->name}}</td>
<input type="hidden" name="hidden_name_{{ $item->id }}" id="hidden_name_{{ $item->id }}" value="{{$item->name}}">
<td>
<input type="number" name="quantity_list_{{ $item->id }}" id="quantity_list_{{ $item->id }}" min="{{$item->quantity == 0 ? 0: 1}}" max="{{$item->quantity}}" onkeypress="return false" {{$item->quantity == 0 ? 'disabled' : ''}}>
</td>
<td>{{ $item->barcode }}
</td>
<input type="hidden" name="hidden_barcode_{{ $item->id }}" id="hidden_barcode_{{ $item->id }}" value="{{ $item->barcode }}">
<td>
<input type="text" name="product_unit_price_{{ $item->id }}" id="product_unit_price_{{ $item->id }}" {{$item->quantity == 0 ? 'disabled' : ''}}>
</td>
<td>
<input type="hidden" name="hidden_cost_{{ $item->id }}" id="hidden_cost_{{ $item->id }}" value="{{ $item->cost }}">
</td>
</tr>
@endforeach
解决方法
您没有输入数组。您有单独的输入,恰好恰好在其名称后附加了某种类型的标识符。因此,没有名为hidden_name_
的输入,而这正是您在请求中所要求的。
由于它们不是数组,您可以尝试使用它们的真实名称来拉出它们:
$request->input('hidden_name_'. $xx)