【问题标题】:Laravel Trying to access array offset on value of type nullLaravel尝试访问null类型值的数组偏移量
【发布时间】:2021-02-02 20:15:18
【问题描述】:

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

【问题讨论】:

  • 那么$request-&gt;hidden_name_、$request-&gt;product_unit_price_、$request-&gt;quantity_list_ 或$request-&gt;hidden_barcode_ 是null 然后
  • dd($request->all())
  • 除了product_item_list 之外没有任何数组,其他一切都只是一个字符串......您正在尝试访问 foreach 中数组的索引,而那些不是数组,这些输入不会以您尝试使用的名称存在
  • 在你的刀片中输入名称需要添加ex:

标签: laravel


【解决方案1】:

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

你可以试着用他们的实际名字来拉他们,因为他们不是数组:

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

【讨论】:

    【解决方案2】:

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

    您收到此错误是因为在控制器循环期间,至少有一次没有值(或空值)存储在偏移数组中,并且当该数组传递给刀片文件时,刀片文件在显示时无法找到任何值给定的偏移数组,因此您得到给定的错误。 虽然刀片文件显示了错误,但它源于控制器文件中给定偏移数组的循环。

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-09
      • 1970-01-01
      • 2021-07-23
      • 2020-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多