Skip to content
Advertisement

Laravel 7 Illegal offset type

I am having an error when I try to when updating multiple names of filters.

public function update(Request $request, Filter $filter)
{
    $filters = collect([$request->filters]);
    $filters->each(function ($item) use ($request, $filter) {
        if (!isset($item['name']) && !isset($item['latin'])) {
            foreach ($item as $key) {
                $data = [
                    'category_id' => $request->category_id,
                    'name' => $item[$key]['name'],
                    'latin' => $item[$key]['latin'],
                    'field' => $item[$key]['field'],
                ];
                $filter->update($data);
            }
        } else {
            return ;
        }
    }
}

When I change this, the first record is updated.

'name' => $item[1]['name'],
'latin' => $item[1]['latin'],
'field' => $item[1]['field'],

I now had three records and I changed all three folds and hit the reserve. The first record changed, the second and the third did not change. I want it to be n.

When I try this

$filters->each(function ($item) use ($request, $filter) {
    if (!isset($item['field'])) {
        dd($item);
    }

I see this message

array:2 [▼
  5 => array:4 [▼
    "name" => "Value RAM"
    "latin" => "ram"
    "field" => "0"
    "value" => array:2 [▼
      0 => "One Gigabayte"
      1 => "Twoo Gigabayte"
    ]
  ]
  6 => array:3 [▼
    "name" => "Color"
    "latin" => "color"
    "field" => "1"
  ]
]

Advertisement

Answer

@irankhostravi you are trying to get the wrong key from the array: You are calling 'latin' => $item[$key]['latin'] but $key is not an integer but the value, because you are looping too many times trough the array.

If I dump the output of the $key this is what I get:

array:3 [▼
    "latin" => "ram"
    "field" => "0"
    "value" => array:2 [▶]
]

// dump($key)
"key: ram"

So you need to remove that extra foreach(), which isn’t necessary. Besides of that, your are checking if ‘name’ and ‘latin’ are not set, if so, then you try to get the value of these non-existing keys..

I’ve refactored some of your code and I think this is what you need:

public function update(Request $request, Filter $filter)
{
    collect($request->filters)
        ->each(function ($item) use ($request, $filter) {
            // Check if the keys exists in the $item, otherwise return.
            // Changed && to || because if one of them is missing, you want to abort,
            // unless your database is accepting empty/nullable values.
            // But please use validation rules here instead: https://laravel.com/docs/master/validation#quick-writing-the-validation-logic
            if (! isset($item['name']) || ! isset($item['latin']) || ! isset($item['field'])) {
                return;
            }

            $filter->update([
                'category_id' => $request->category_id,
                'name' => $item['name'],
                'latin' => $item['latin'],
                'field' => $item['field'],
            ]);
        });
}

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement