0

I'm working with Laravel, and I have an Object that I must push to my DB, the thing is, this is an Object, and I need it to be an array to push it in, Ill explain here:

so, this is how Im pushing in the array to my DB:

$subset = collect($MYARRAY);
        $arr = $subset->map (function ($item) {
            $now =  Carbon::now('utc')->toDateTimeString();
            return array(
                'order_id' => $item['data']['AmazonOrderId'],
                'buyer_name' => $item['data']['BuyerName'],
                'buyer_email' => $item['data']['BuyerEmail'],
                'store_name' => $item['storeName'],
                'ship_service_level' => $item['data']['ShipServiceLevel'],
                'order_status' => $item['data']['OrderStatus'],
                'fulfillment_channel' => $item['data']['FulfillmentChannel'],
                'purchase_date' => $item['data']['PurchaseDate'],
                'last_ship_date' => $item['data']['LatestShipDate'],
                'created_at'=> $now,
                'updated_at'=> $now
            );
        })->all();

        $order->insert($arr);

This code is working great, If I have this $MYARRAY (I created the array manually):

$MYARRAY = array (
            0 =>
                array(
                    'data' =>
                        array ( 
                        [AmazonOrderId] => 111
                        [SellerOrderId] => 222
                        [PurchaseDate] => 2013-05-31T15:20:59Z
                        [LastUpdateDate] => 2013-06-01T18:00:41Z
                        ... 
                        ))
            1 =>
                array(
                    'data' =>
                        array (
                          ...
                                )
);

But, it fails in this object (this is var_export of the object):

Array
(
    [0] => Sonnenglas\AmazonMws\AmazonOrder Object
        (
            [data:Sonnenglas\AmazonMws\AmazonOrder:private] => Array
                (
                    [AmazonOrderId] => 111
                    [SellerOrderId] => 222
                    [PurchaseDate] => 2013-05-31T15:20:59Z
                    [LastUpdateDate] => 2013-06-01T18:00:41Z
                    ...
                )
        )

    [1] => Sonnenglas\AmazonMws\AmazonOrder Object
        (
            [data:Sonnenglas\AmazonMws\AmazonOrder:private] => Array
                (
                    [AmazonOrderId] => 2345
                    [SellerOrderId] => 2345
                    [PurchaseDate] => 2013-06-01T02:11:24Z
                   ...

        ))

Now, this is what I have tried, I tried to convert it to array like that:

$array = json_decode(json_encode($object), true);

but then I had an empty arrays (the conversion to json and back deleted it all because of the object type I guess).

I have tried this one as well:

$MYARRAY = array_map(function($object){
            return (array) $object;
        }, $data);

With no success, (I have tried also to cast it to array with foreach) so, can you please help, how can I convert this object back to array, or, how can I map the object to push it in the DB?

Thank you!

6
  • Have you tried the toArray() method? It is available in all models by extending the base class Model Commented Jul 1, 2017 at 15:24
  • Is github.com/Tapha/mws-laravel/blob/master/src/Classes/… the class you're using? If so it's got a getData() method which will return the data array. As you can see that member is private so you can't just grab it without using the class public members. Commented Jul 1, 2017 at 15:24
  • The general solution to that is to write a so called mapper. This is nothing specific to Laravel btw.. Commented Jul 1, 2017 at 15:30
  • I have tried toArray(), it doesn't work on this object from some reason, @apokryfos -> Ill try it now, Thank you! Commented Jul 1, 2017 at 15:33
  • @hakre the issue here is more likely to do that OP is trying to directly access a private class member. Commented Jul 1, 2017 at 15:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.