dongsheng8158 2018-12-02 02:46
浏览 92

使用sizeof时,Laravel Array转换为字符串

i'm new on laravel, basically when i using codeigniter this code works fine. The problem is i cannot use this for insert data.

for($count = 0; $count < sizeof($cid); $count++){
inset to table for item 1,
inset to table for item 2, 
inset to table for item 3,
}

this is my controller

   $request->validate([
        'pembelian_kode' => 'required',
        'barang_kode' => 'required',
        'pembelian_total' => 'required',
        'pembelian_qty' => 'required',
        'supplier_id' => 'required',
      ]);

      $cid = Input::POST('cid');
      $cg = PembelianModel::create($request->all());

      if($cg){
        for($count = 0; $count < sizeof($cid); $count++){
          DB::table('pembelian_details')->insert([
            'pembelian_kode' => $request['pembelian_kode'], 'barang_kode' => $request['barang_kode'], 'pd_qty' => $request['pembelian_qty']]
          );
        }
      }
      return redirect()->route('pembelians.index')
      ->with('success','Data berhasil ditambah');

reults error 
Array to string conversion (SQL: insert into `pembelian_details` (`pembelian_kode`, `barang_kode`, `pd_qty`) values (PBL1812025877, BRG10181125230, 10))

so i have two tables 1 is 'orders' and also 'order_details'. For the table orders it has been successful in adding data, but for orders details there are still errors.

can someone help me ?

  • 写回答

1条回答 默认 最新

  • dongtaoxue4674 2018-12-02 06:55
    关注

    I just modified your script .Hope this will work

    Problems:

    $request['barang_kode']
    
    • Missing index

    • $request is an object not array

           $cid = $request->cid;
              $cg = PembelianModel::create($request->all());
      
              if($cg){
                  for($count = 0; $count < count($cid); $count++){
                      DB::table('pembelian_details')->insert([
                              'pembelian_kode' => $request->pembelian_kode[$count],
                              'barang_kode' => $request->barang_kode[$count],
                              'pd_qty' => $request->pembelian_qty[$count]
                          ]
                      );
                  }
              }
              return redirect()->route('pembelians.index')
                  ->with('success','Data berhasil ditambah');
      
    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器