0

Below is the array. I would be like to insert the array into my "sells" table by Laravel eloquent ORM system. but

$data = [
                'products' => [
                    'product_id' => ['1', '3', '4'],
                    'quantity' => ['5', '5', '4'],
                    'product_discount' => ['30', '0', '5'],
                    'discount_unit' => ['%', 'tk', '%'],
                ],

                'customer_id' => 1,
                'total_discount' => 50,
                'shiping_charge' => 10,
                'net_payable' => 5000,

                'payments' => [
                    'payment_method' => ['cash', 'card', 'check'],
                    'payment_amount' => ['500', '7000', '400'],
                ],
            ];

Here is the Eloquent Code to insert data into sells table.

$sell = new Sell();

    $sell->sells_code = 'SCN-' . rand(time(), 1000);

    $sell->sells_date = date('Y-m-d');

    foreach ($data['products']['product_id'] as $value){
        $sell->product_id = $value . ",";
    }



    foreach ($data['products']['quantity'] as $value){
        $sell->product_quantity = $value . ",";
    }

    foreach ($data['products']['product_discount'] as $value){
        $sell->product_discount = $value . ",";
    }

    foreach ($data['products']['discount_unit'] as $value){
        $sell->product_discount_unit = $value . ",";
    }


    $sell->customer_id = $data['customer_id'];
    $sell->company_name = 'Undefined';
    $sell->warehouse_id = 1;
    $sell->total_discount = $data['total_discount'];

    $sell->shipping_address = 'Undefined Address';
    $sell->shipping_charge = 0;
    $sell->net_payable = $data['net_payable'];


    foreach ($data['payments']['payment_method'] as $value){
        $sell->payment_method = $value . ",";
    }

    foreach ($data['payments']['payment_amount'] as $value){
        $sell->payment_amount = $value . ",";
    }


    $sell->payment_date= date('Y-m-d');
    $sell->note = 'Nothing';
    $sell->sells_Status = 'Success';
    $sell->biller = 'Mahady';


    $sell->save();

Are you please to share the effective and easiest way to insert array value into a mysql database table?

3
  • You're wanting to concatenate product_id instead of storing it as a proper many-to-many relationship? Commented Aug 17, 2018 at 12:38
  • How do you exactly save the data? Commented Aug 17, 2018 at 12:41
  • No relationship is needed here. I just want to insert data from the array into my sells table. Commented Aug 17, 2018 at 12:56

1 Answer 1

1

First thing you are not concatenating product_id, you are actually assigning new value to $sell->product_id on each iteration.

This could help you

foreach ($data['products']['product_id'] as $value){
        $sell->product_id .= $value . ",";
}

or you can convert this array to json string and then you can store it in you table column

$sell->product_id = json_encode($data['products']['product_id'])

and on retrieving from db, simple do this, and you will get your array of id's

json_decode($sell->product_id)
Sign up to request clarification or add additional context in comments.

3 Comments

You'll want .= for string concatenation. += is only for numbers in PHP.
yeah, sorry just a typo mistake.
Thank you very much. The json_encode and json_decode is the most perfect and easiest way. It is working.

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.