skip to Main Content

Trying to access array offset on the value of type null in the controller

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];

where The dd($request->product_item_list) like

array:1 [▼
  0 => "13"
]

How can I fix the problem?
PHP version 7.4.6

dd($request->all()) looks like

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

2

Answers


  1. You don’t have arrays of inputs. You have individual inputs that just happen to have some type of identifier appended to their name. So there is no input named hidden_name_ which is what you are asking for from the Request.

    You can try to just pull them by their actual name since they are not arrays:

    $request->input('hidden_name_'. $xx)
    
    Login or Signup to reply.
  2. Lets Understand the error first. It says that array offset has null value.
    In the controller you are running a loop and adding value in the array and then you are passing the array to blade file and displaying its value via blade file.

    You are getting this error because during the looping in controller atleast once No Value(or Null value) is getting stored in offset array and when that array is passed to blade file, Blade file is not able to find any value while displaying that given offset array and hence you are getting the given error.
    Although error is shown by blade file but it is originating in the loop of that given offset array in the controller file.

    For solution you need to find in which iteration null value is getting stored in offset array.
    After that you need to check whether null value is possible at all or not(as per business logic of project) during the loop cycle.
    If null value is not possible(as per business logic of project) then something is wrong with the logic of coding because of which null value is getting stored in array. and you need to fix that logic of loop in controller file to ensure that no null values are stored in array.

    If business logic allows null value then you need to handle the array accordingly, first in controller file and then in blade file.

    In blade file you can apply a ‘IF’ condition to check if offset array value is null or not and take preventive steps before displaying the value

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search