1

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.

2
  • just write $item = json_decode(json_encode($item),true); before return [ for solving it for now. If you want to opt for a more greater solution read about Objects and array :) Commented Jul 30, 2019 at 10:56
  • Simply use $item->name. Commented Jul 30, 2019 at 11:53

1 Answer 1

0

You can use the Type Casting object to an array like this for exemple :

 protected function setCartItems($items)
{
    return (new Collection($items))->map(function ($item, $num) {
        $itemArray = (array) $item;
        return [
            'L_PAYMENTREQUEST_0_NAME'.$num  => $itemArray ['name'],
            'L_PAYMENTREQUEST_0_AMT'.$num   => $itemArray ['price'],
            'L_PAYMENTREQUEST_0_DESC'.$num  => isset($itemArray ['desc']) ? $itemArray ['desc'] : null,
            'L_PAYMENTREQUEST_0_QTY'.$num   => isset($itemArray ['qty']) ? $itemArray ['qty'] : 1,
        ];
    })->flatMap(function ($value) {
        return $value;
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.