我是laravel的新手,基本上,当我使用codeigniter时,此代码可以正常工作。 问题是我无法将其用于插入数据。
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,
}
这是我的控制器
$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))
所以我有两个表1是'orders'也是'order_details'。对于表订单,已成功添加数据,但对于订单详细信息,仍然存在错误。
有人可以帮助我吗?
答案 0 :(得分:0)
我刚刚修改了您的脚本。希望这会起作用
问题:
$request['barang_kode']
缺少索引
$ request是一个对象而不是数组
$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');