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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With