samedi 1 juillet 2017

How to push an object to laravel db?

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!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire