'How to store some field as Json when importing Excel in Laravel
I try to import Excel to Laravel when some field merges as JSON in the customer's table. In my case, I try to merge some fields like berar_badan, usia, etc., on the other_data field. I tried this code but failed.
$this->validate($request, [
'file' => 'required|mimes:csv,xls,xlsx'
]);
$file = $request->file('file');
$data = Excel::toArray(new CustomerImport, $file);
DB::beginTransaction();
try {
foreach ($data[0] as $d) {
//skip header
if ($d == 0) {
continue;
}
$email = Customer::where('email', $d['email_address'])->first();
$otherData = array_shift($d);
if (!$email) {
$customer = Customer::create([
// 'source' => $d['source'],
'source_detail' => $d['source_detail'],
'source_sub_detail' => $d['source_detail'],
'name' => $d['customer_name'],
'phone_1' => $d['phone_number_1'],
'email' => $d['email_address'],
'dob' => $d['dob'],
'address' => $d['address'],
'village' => $d['kelurahan'],
'district' => $d['kecamatan'],
'regency' => $d['kabupaten'],
'province' => $d['provinsi'],
'hpl' => $d['hpl'],
'parent_type' => $d['parent_type'],
'password' => Hash::make($d['phone_number_1']),
'client_id' => Session::get('client_id'),
'other_data' => $otherData
]);
DB::table('customer_children')->insert([
'customer_id' => $customer->id,
'name' => $d['child_name'],
'dob' => $d['child_dob'],
'gender' => $d['children_gender'],
'created_at' => Carbon::now(),
]);
}
}
DB::commit();
Alert::success('Success', 'Success upload data customer.');
return redirect()->back();
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
