Laravel正在尝试访问类型为null的值的数组偏移量



试图访问控制器中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"
]

我该如何解决这个问题?PHP 7.4.6版

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

让我们先了解错误。它表示数组偏移量为null值。在控制器中,您正在运行一个循环并在数组中添加值,然后将数组传递到刀片文件并通过刀片文件显示其值。

您之所以会出现此错误,是因为在控制器中循环期间,偏移数组中至少存储了一次"无值"(或"空值"(,并且当该数组传递给刀片文件时,刀片文件在显示给定偏移数组时找不到任何值,因此您会出现给定错误。虽然刀片文件显示了错误,但它源于控制器文件中给定偏移阵列的循环。

对于解决方案,您需要找到哪个迭代null值存储在偏移数组中。之后,您需要在循环周期中检查null值是否可能(根据项目的业务逻辑(。如果null值是不可能的(根据项目的业务逻辑(,那么编码逻辑就出了问题,因为哪个null值被存储在数组中。您需要修复控制器文件中循环的逻辑,以确保数组中没有存储null值。

如果业务逻辑允许空值,那么您需要相应地处理数组,首先在控制器文件中,然后在刀片文件中。

在刀片文件中,您可以应用"IF"条件来检查偏移数组值是否为空,并在显示值之前采取预防措施

您没有输入数组。您有一些单独的输入,它们的名称后面恰好附加了某种类型的标识符。因此,没有名为hidden_name_的输入,这正是您从请求中所要求的。

你可以试着只按它们的实际名称提取它们,因为它们不是数组:

$request->input('hidden_name_'. $xx)

最新更新