I'm trying to take items from my database and put them into the format required by the "laravel-paypal" package:
$data['items'] = [
[
'name' => 'Product 1',
'price' => 9.99,
'desc' => 'Description for product 1'
'qty' => 1
],
[
'name' => 'Product 2',
'price' => 4.99,
'desc' => 'Description for product 2',
'qty' => 2
]
];
So I have a sweet query:
$order_items = DB::table('order_items')
->where('order_id', $order['id'])
->join('products', 'order_items.product_id', '=', 'products.id')
->selectRaw('products.name, order_items.price + order_items.tax as price, order_items.quantity as qty')
->get()
->toArray();
Which yields:
array:2 [▼
0 => {#296 ▼
+"name": "fugit"
+"price": 727.82
+"qty": 1
}
1 => {#298 ▼
+"name": "MEMBERSHIP"
+"price": 35.0
+"qty": 1
}
]
But when I try to put it into the required array:
$data = [];
$data['items'] = $order_items;
I get the error message:
Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_ERROR) Cannot use object of type stdClass as array
With details of:
protected function setCartItems($items)
{
return (new Collection($items))->map(function ($item, $num) {
return [
'L_PAYMENTREQUEST_0_NAME'.$num => $item['name'],
'L_PAYMENTREQUEST_0_AMT'.$num => $item['price'],
'L_PAYMENTREQUEST_0_DESC'.$num => isset($item['desc']) ? $item['desc'] : null,
'L_PAYMENTREQUEST_0_QTY'.$num => isset($item['qty']) ? $item['qty'] : 1,
];
})->flatMap(function ($value) {
return $value;
});
I've read all the solutions for this error message, and they all say that what I have is an array of objects, not an array of arrays, which is why I'm getting this message. I get it. They all say that I have to access the data with $order_items->price
. I get it.
But I need the data in that array of arrays format and since it's a nested array, I can't even figure out how I would do it with a foreach loop.
Any help would be appreciated.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire