我要保存由create.blade.php视图生成的数据,
按下提交按钮后,控制台日志输出类似于以下代码
{ "kode_trx": "PO-2019030004", "no_po": "0005/DIN/III/2019", "pic_po": "Carlo Donadini", "pic_telp": "+39438778020", "top": "RRRRR", "supplier_id": "1", "delivery_date": "28-03-2019", "created_by_id": "1", "total_barang": "7", "grand_total": "122500000", "tb_detail_po": [ [ "BRG-2019030004", "Electric Deep Fryer", "SC-81", "290", "440", "310", "4", "17500000", "70000000", " ", "4", "PO-2019030004", "0005/DIN/III/2019", "1", "5" ], [ "BRG-2019030004", "Electric Deep Fryer", "SC-81", "290", "440", "310", "3", "17500000", "52500000", " ", "4", "PO-2019030004", "0005/DIN/III/2019", "1", "5" ] ] }
所以我想将其保存到2个不同的表,表购买和purchase_details
我该如何在方法存储区的POController中编写代码? ???
答案 0 :(得分:0)
$result = $request->all();
$created = Your Model::create([Your Data For Table 1]);
if($created) {
foreach($request->tb_detail_po as $details) {
Your Model2::create([Your Detail Data For Table 2]);
}
}
答案 1 :(得分:0)
您可以像下面那样将数据存储在表purchase
上
class POController extends Controller
{
public function store(Request $request)
{
$purchase = Purchase::create([
"id_penerimaan" => $request->no_nota,
"id_customer" => $request->customer,
"id_karyawan" => $request->karyawan,
"tgl_penerimaan" => \Carbon\Carbon::now()->format('Y-m-d H:i:s'),
]);
return $purchase;
}
}
对于表purchase_details
,如果您使用Observer,那就太好了
观察者示例:
class UserObserver
{
public function created(Purchase $purchase)
{
if (request()->tb_detail_po) {
foreach(request()->tb_detail_po as $detail) {
PurchaseDetail::create([
'id_purchase' => $purchase->id,
'id_penerimaan' => request()->no_nota,
'id_karyawan' => $detail[9],
'id_kategori' => $detail[8],
'sn_barang' => $detail[1],
'nama_barang' => $detail[0],
'kelengkapan' => $detail[2],
'keluhan' => $detail[3],
'id_garansi' => $detail[10],
'id_status' => '1',
'kondisi' => '1'
]);
}
}
}
}
答案 2 :(得分:0)
Try to use **JSON_DECODE($data)** OR **JSON_ENCODE($data)**