Insert Multiple table ke Database Menggunakan Ajax pada laravel

Screenshot_33.png21.14 KBini adalah data yang berhasil saya dapatkan. ini menggunakan seralizeArray.

 $('#form_field').submit(function(e) {
         e.preventDefault()
         const serialize = $(this).serializeArray();
         const table_item = document.querySelectorAll('#tabel_item_stock_entry tbody');
         let array_item_quotation = [];
         const el = $("#tabel_item_stock_entry tbody").find("tr");
         for (let i = 0; i < el.length; i++) {
               const els = $(`#tabel_item_stock_entry tbody tr:eq(${i})`);
               const item_name = els.find(".item_name").text();
               const qty = els.find(".qty").val();
               const rate = els.find(".rate").val();
               const amount = els.find(".amount").val();
               // const _token = els.find('meta[name="csrf-token"]').attr('content');
               array_item_quotation.push({
                  item_name: item_name,
                  qty: qty,
                  rate: rate,
                  amount: amount
               });
               $.ajaxSetup({
                  headers: {
                     'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                  }
               });
               $.ajax({
                  url: "/quotation-list",
                  type:"POST",
                  data:{
                     item_name:item_name,
                     qty:qty,
                     rate:rate,
                     amount:amount,
                     body:serialize
                  },
                  success:function(response){
                     // if(response.success){
                     //       alert(response.message) //Message come from controller
                     // }else{
                     //       alert("Error")
                     // }
                  },
                  error:function(error){
                     // console.log(error)
                  }
               });
         }
         console.log( $(this).serializeArray(), array_item_quotation );
      });

ini script jquery yang saya buat

public function store(Request $request)
    {
        $data = $request->all();
        $objects = json_decode(json_encode($data));

        $objects = new Quotation();
        $objects->series = $request->series;
        $objects->customer = $request->customer;
        $objects->posting_date = $request->posting_date;
        $objects->grand_total_amount = $request->grand_total_amount;
        $objects->total_qty = $request->total_qty;
        $objects->grand_total = $request->grand_total;
        $objects->save();

        $data2 = DB::table('quotation_items')->insert([
            'quotation_id' => $objects->id,
            'item_name' => $request->input('item_name'),
            'qty' => $request->input('qty'),
            'rate' => $request->input('rate'),
            'amount' => $request->input('amount'),
            'created_at' => Carbon::now(),
            'updated_at' => Carbon::now()
        ]);

        return response()->json(['success'=>'Ajax request submitted successfully']);
    }

ini Controller yang saya buat, dan selalu menampilkan eror seperti ini "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'series' cannot be null (SQL: insert into `quotations` (`series`, `customer`, `posting_date`, `grand_total_amount`, `total_qty`, `grand_total`, `updated_at`, `created_at`) values (?, ?, ?, ?, ?, ?, 2021-07-11 09:23:29, 2021-07-11 09:23:29))",

" Isi value selalu kosong, Boleh minta solusi?

avatar nurifan
@nurifan

2 Kontribusi 0 Poin

Diperbarui 2 tahun yang lalu

2 Jawaban:

<div>Daru errornya berarti kolom "series" nya kosong. Coba cek lagi apakah kamu sudah mengirim data untuk seriesnya</div>

avatar hilmanski
@hilmanski

2670 Kontribusi 2132 Poin

Dipost 2 tahun yang lalu

Tanggapan

ini adalah data yang saya console. ini menggunakan serializeArray, contoh jquery saya sepert yang sudah saya tampilkan di atas dan juga controller saya seperti yang saya tampilkan di atas. Bagaimana caranya agar saya bisa mendapatkan data series, customer, posting_date dan lain-lain yang berupa array seperti gambar di bawah ini, agar bisa saya oleh di controller. mohon bantuannya terimakasih

<div><br><br>ini adalah data yang saya console. ini menggunakan serializeArray, contoh jquery saya sepert yang sudah saya tampilkan di atas dan juga controller saya seperti yang saya tampilkan di atas. Bagaimana caranya agar saya bisa mendapatkan data series, customer, posting_date dan lain-lain yang berupa array seperti gambar di bawah ini, agar bisa saya oleh di controller. mohon bantuannya terimakasih<br><figure data-trix-attachment="{&quot;contentType&quot;:&quot;image/png&quot;,&quot;filename&quot;:&quot;Screenshot_34.png&quot;,&quot;filesize&quot;:122603,&quot;height&quot;:296,&quot;url&quot;:&quot;https://i.ibb.co/crmj2YK/Screenshot-34.png&quot;,&quot;width&quot;:1364}" data-trix-content-type="image/png" data-trix-attributes="{&quot;presentation&quot;:&quot;gallery&quot;}" class="attachment attachment--preview attachment--png"><img src="https://i.ibb.co/crmj2YK/Screenshot-34.png" width="1364" height="296"><figcaption class="attachment__caption"><span class="attachment__name">Screenshot_34.png</span></figcaption></figure><br><br></div>

avatar nurifan
@nurifan

2 Kontribusi 0 Poin

Dipost 2 tahun yang lalu

Login untuk ikut Jawaban