Kobid Kunda
Kobid Kunda

Reputation: 47

I am trying to post json invoice data i.e inv_id , Name etc & product data i.e item_id, item_name etc in database but getting error

I am trying to save an invoice along with product in different table using jquery and laravel. i am getting error on foreach looping

error i am getting is : line 42 is foreach line

"message": "Trying to get property of non-object",
"exception": "ErrorException",
"file": "/Volumes/OTHERS/Projects/invoicing/invoicing/app/Http/Controllers/ManageInvoice.php",
"line": 42,

my controller :

public function store(Request $request)
{
    $s = new Invoice();
    $s->inv_id = $request->inv_id;
    $s->inv_date_of_purchase = $request->inv_date_of_purchase;
    $s->inv_return_month = $request->inv_return_month;
    $s->inv_due_date = $request->inv_due_date;
    $s->inv_customer_name = $request->inv_customer_name;
    $s->inv_customer_gstin = $request->inv_customer_gstin;
    $s->inv_customer_state = $request->inv_customer_state;
    $s->inv_customer_bill_address = $request->inv_customer_bill_address;
    $s->save();

    $data =  $request->items;



    foreach($data->item_name as $key => $value) {
        InvoiceItem::create(['item_name'=>$value]);
    }

    return response()->json(['success'=> $data]);



}

Blade jQuery post request:

$('#invsubmit').click( function(e) {
        e.preventDefault();
        var rows = [];
        $('#inv_table tbody tr').each(function(i, n){
            var $row = $(n);
            rows.push({
                item_id: $row.find('td:eq(0)').text(),
                item_name: $row.find('td:eq(1)').text(),
                item_type: $row.find('td:eq(2)').text(),
                item_hsn_sac: $row.find('td:eq(3)').text(),
                item_qty: $row.find('td:eq(4)').text(),
                item_unit: $row.find('td:eq(5)').text(),
                item_rate: $row.find('td:eq(6)').text(),
                item_tax_value: $row.find('td:eq(7)').text(),
                item_tax_rate_percentage: $row.find('td:eq(8)').text(),
                item_tax_cgst_percentage: $row.find('td:eq(9)').text(),
                item_tax_sgst_percentage: $row.find('td:eq(10)').text(),
                item_tax_igst_percentage: $row.find('td:eq(11)').text(),
                item_tax_cess_percentage: $row.find('td:eq(12)').text(),
                item_tax_cess: $row.find('td:eq(13)').text(),
                item_total: $row.find('td:eq(14)').text()
            });
        });


        var _token = $("input[name='_token']").val();
        var inv_id = $("input[name='inv_id']").val();
        var inv_date_of_purchase = $("input[name='inv_date_of_purchase']").val();
        var inv_return_month = $("input[name='inv_return_month']").val();
        var inv_due_date = $("input[name='inv_due_date']").val();
        var customername = $("input[name='customername']").val();
        var inv_customer_gstin = $("input[name='inv_customer_gstin']").val();
        var inv_customer_state = $("input[name='inv_customer_state']").val();
        var inv_customer_bill_address = $("textarea[name='inv_customer_bill_address']").val();
        var invdata = $('#inv_table td').text();
          var d = {
            _token:_token,
            inv_id:inv_id,
            invoice_id: inv_id,
            inv_date_of_purchase:inv_date_of_purchase,
            inv_return_month:inv_return_month,
            inv_due_date:inv_due_date,
            customername:customername,
            inv_customer_gstin:inv_customer_gstin,
            inv_customer_state:inv_customer_state,
            inv_customer_bill_address:inv_customer_bill_address,
            items:rows

        };
        console.log(JSON.stringify(d));

        $.ajax({
            url: "{{route('inv.store.post')}}",
            type:'POST',
            data: d
            });

       // alert(data);


    });

json data i am gettiing on alert(data)

{
"_token": "hjaWX7HS33eJ7pKyEsfX9xW6neCxNrOJU6CJ38aV",
"inv_id": "",
"invoice_id": "",
"inv_date_of_purchase": "03/01/2018",
"inv_return_month": "01/2018",
"inv_due_date": "03/01/2018",
"inv_customer_gstin": "2155552233",
"inv_customer_state": "West Bengal",
"inv_customer_bill_address": "street name , locality, city state pin country",
"items": [{
    "item_id": "1",
    "item_name": "Amul Taza",
    "item_type": "",
    "item_hsn_sac": "3132518",
    "item_qty": "1",
    "item_unit": "cm",
    "item_rate": "24",
    "item_tax_value": "1.2",
    "item_tax_rate_percentage": "5",
    "item_tax_cgst_percentage": "2.5",
    "item_tax_sgst_percentage": "2.5",
    "item_tax_igst_percentage": "5",
    "item_tax_cess_percentage": "",
    "item_tax_cess": "",
    "item_total": "24"
}, {
    "item_id": "2",
    "item_name": "Amul Taza",
    "item_type": "",
    "item_hsn_sac": "3132518",
    "item_qty": "1",
    "item_unit": "cm",
    "item_rate": "24",
    "item_tax_value": "1.2",
    "item_tax_rate_percentage": "5",
    "item_tax_cgst_percentage": "2.5",
    "item_tax_sgst_percentage": "2.5",
    "item_tax_igst_percentage": "5",
    "item_tax_cess_percentage": "",
    "item_tax_cess": "",
    "item_total": "24"
}]

}

route:

Route::post('/invoice', 'ManageInvoice@store')->name('inv.store.post');

I want to post invoice details in invoice table and item details in the invoice_item table

Upvotes: 0

Views: 98

Answers (1)

aresklip
aresklip

Reputation: 86

$data = $request->input('items');

foreach($data as $key => $value) {
    InvoiceItem::create(['item_name' => $value['item_name']]);
}

Upvotes: 0

Related Questions