Indirect modification of overloaded element has no effect
First off, you're missing get()
in your code, so either:
1 You are iterating over the Query\Builder
instead of the array of results, because you never executed the query. (I suppose you forgot it only here, because otherwise you would probably get trying to get property of non-object
)
or 2 one of the rows has ''
or null
value in the field_group_name
column.
That's why your code does this:
$item['fields'][NULL] = [];
and that's causing Indirect modification ...
error.
So add check for empty value first:
if ($f->field_group_name && ! isset($item['fields'][$f->field_group_name]))
$item['fields'][$f->field_group_name] = [];
you may need to adjust it to your needs, but you get the idea.
Comments
-
eComEvo over 1 year
I have the following Eloquent query:
$item = Item::where('sku', $sku)->first();
After this query comes in I'm adding a variety of elements manually such as:
$item['total'] = $item['subtotal'] + $this->currentInventory();
Statements like the above that modify the object work just fine.
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
$item['fields'][$f->field_group_name] = [];
to produce the 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 an actual element to the$item['fields']
array that I get this error?PHP version 5.6.0.
-
eComEvo over 9 yearsYes, I left out the
get()
on this post. Fixed. Added a check, but non-empty values still produce this error. -
eComEvo over 9 yearsI did notice that if I switch to filling another variable with the field data (such as
$fields
) and then do$item['fields'] = $fields;
after the loop completes then that works fine. This is the workaround I had to do, but I'm still baffled why this would be necessary in the first place. -
eComEvo over 9 yearsThat may be it. Seems to fit the workaround I had to do. Good detective work!