Indirect modification of an overloaded element does not affect

I have the following Eloquent request:

$item = Item::where('sku', $sku)->first();

After this request, I add several items manually, for example:

$item['total'] = $item['subtotal'] + $this->currentInventory();

Statements like the ones above that change the facility very well.

It stops working when I do the following:

$item['fields'] = [];

$fields = DB::table('item_fields')->where('item_id', $item['id'])->get();

foreach ($fields as $f) {
    if (!isset($item['fields'][$f->field_group_name]))
         $item['fields'][$f->field_group_name] = [];

    $item['fields'][$f->field_group_name]['valid_values'] = DB::table('item_field_valid_values')->where('item_field_id', $f->item_field_id);
}

This will cause the line to $item['fields'][$f->field_group_name] = [];throw an error:

Indirect modification of overloaded element of Item has no effect

How can it be that I can assign $item['fields'] = [], but when I try to add the actual element to the array $item['fields']that I get this error?

PHP version 5.6.0.

+4
source share
1 answer

First, you are missing out get()on your code, so either:

1 Query\Builder , . ( , , , , trying to get property of non-object)

2 '' null field_group_name.

:

$item['fields'][NULL] = [];

Indirect modification ....

, :

if ($f->field_group_name && ! isset($item['fields'][$f->field_group_name]))
     $item['fields'][$f->field_group_name] = [];

, .

+2

Source: https://habr.com/ru/post/1570543/


All Articles