0

I have Json Data like this :

$data = '[
    {
        "OrderId": "1038806370",
        "qtty": "1",
        "Item": "Strawberry 250 gr",
        "SKU": "20091"
    },
    {
        "OrderId": "1038806370",
        "qtty": "2",
        "Item": "Strawberry 130 gr",
        "SKU": "20092"
    },
    {
        "OrderId": "1038806370",
        "qtty": "1",
        "Item": "Strawberry 130 gr",
        "SKU": "20092"
    }
]';

and I want to Save in my database MySql using PHP Laravel,...

I Want To save data from json to my table, there are table Order values [orderID,...] and OrderDetail values [orderID, SKU, Qty]

This is my code in controller

$order = json_decode($data, true);

foreach ($order as $ord) {
    $check = Order::where('orderid', $ord['OrderId'])->get();

    if (count($data) > 0) {
        OrderDetail::firstOrCreate([
            'order_id' => $ord['OrderId'],
            'sku_id' => $ord['SKU'],
            'qty' => $ord['qtty']
        ]);
    } else {
        Order::create([
            'orderid'=> $ord['OrderId'],
        ]);
       
        OrderDetail::firstOrCreate([
            'order_id' => $ord['OrderId'],
            'sku_id' => $ord['SKU'],
            'qty' => $ord['qtty']
        ]);
    }
}

but i dont get what i want, I get QTY of SKU 20091 is 2 but actual json data is 3

3
  • Your $data is missing the array brackets: [{...},{...}] Commented Dec 8, 2020 at 17:56
  • how about save data ? @MaartenVeerman Commented Dec 8, 2020 at 18:18
  • You check the count of $data, you should check the count of $check Commented Dec 8, 2020 at 18:23

1 Answer 1

1

I'm not sure why you're counting the $data instead of $check variable. You don't really need the $check variable either though. I'll rewrite your code below.

$order = json_decode($data, true);

foreach ($order as $ord) {
    // use EXIST query to check if Order exists or not.
    if (Order::where('orderid', $ord['OrderId'])->exists())
        OrderDetail::firstOrCreate([
            'order_id' => $ord['OrderId'],
            'sku_id' => $ord['SKU'],
            'qty' => $ord['qtty']
        ]);
    } else {
        Order::create([
            'orderid'=> $ord['OrderId'],
        ]);

        OrderDetail::firstOrCreate([
            'order_id' => $ord['OrderId'],
            'sku_id' => $ord['SKU'],
            'qty' => $ord['qtty']
        ]);
    }
}

But your if/else could also be removed by using the firstOrCreate() method.

$orders = json_decode($data, true);

foreach ($orders as $order) {
    // Get Order Model with orderid = $order['OrderId'] or create it if it doesn't exist
    $model = Order::firstOrCreate([
        'orderid'=> $order['OrderId']
    ]);
    // Get OrderDetail with provided SKU, qtty and orderid or create it if it doesn't exist. 
    OrderDetail::firstOrCreate([
        'order_id' => $model->orderid
        'sku_id' => $order['SKU'],
        'qty' => $order['qtty']
    ]);
}

You could also do it inline.

$orders = json_decode($data, true);

foreach ($orders as $order) {
    OrderDetail::firstOrCreate([
        'order_id' => Order::firstOrCreate(['orderid'=> $order['OrderId']])->orderid,
        'sku_id'   => $order['SKU'],
        'qty'      => $order['qtty'],
    ]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hey, I have new issue when used inline code in here stackoverflow.com/questions/65231289/… can you check it out too ?

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.