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?