Reputation: 794
After a database query, I have a variable $orderProducts
which when I print_r
it, it looks like this below. I am trying to add a field to it (but not save it) before the frontend consumes it. I am trying to loop through it like this:
$orderProducts = Order::with('customer', 'orderProducts.inventory_id')->find(260);
foreach ($orderProducts as $orderProduct) {
$orderProduct->scanned = "N/A";
}
I keep getting the attempt to assign property of non-object error
though. Am I approaching this wrong?
$orderProducts
=> App\Models\Order {#1856
id: 260,
customer_id: 36,
orderProducts: Illuminate\Database\Eloquent\Collection {#1871
all: [
App\Models\OrderProduct {#1877
id: 361,
order_id: 260,
product_id: 13,
inventory_id: 223,
},
App\Models\OrderProduct {#1877
id: 361,
order_id: 260,
product_id: 13,
inventory_id: 223,
},
App\Models\OrderProduct {#1877
id: 361,
order_id: 260,
product_id: 13,
inventory_id: 223,
},
]
}
}
Upvotes: 1
Views: 737
Reputation: 12939
As you can see from here
$orderProducts = Order::with('customer', 'orderProducts.variant')->find(260);
$orderProducts
is the order itself and not the products associated to that order; instead you might do something like this:
$order = Order::with('customer', 'orderProducts.variant')->find(260);
$orderProducts = $order->orderProducts;
foreach ($orderProducts as $orderProduct) { ... }
Upvotes: 5