@tangtang
This is import code:
<?php
namespace App\Imports;
use Carbon\Carbon;
use App\Vouchers;
use Maatwebsite\Excel\Concerns\ToModel;
use Maatwebsite\Excel\Concerns\WithHeadingRow;
use Maatwebsite\Excel\Concerns\WithBatchInserts;
use Maatwebsite\Excel\Concerns\WithChunkReading;
class VouchersImport implements ToModel, WithHeadingRow, WithBatchInserts, WithChunkReading
{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
private function transformDateTime($value, $format = 'Y-m-d')
{
try {
return Carbon::instance(\PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($value))->format($format);
} catch (\ErrorException $e) {
return Carbon::createFromFormat($format, $value);
}
}
public function model(array $row)
{
//dd($row);
return new Vouchers([
'voucher_code' => $row['voucher_code'],
'voucher_value' => $row['voucher_value'],
'batch' => $row['batch'],
'expired' => $this->transformDateTime($row['expired']),
'received_date' => $row['received_date'],
'customer_address' => $row['customer_address'],
'status' => $row['status'],
]);
}
public function batchSize(): int
{
return 1000;
}
public function chunkSize(): int
{
return 1000;
}
}
This is controller:
<?php
namespace App\Http\Controllers;
use App\Vouchers;
use App\Stores;
use App\Exports\VouchersExport;
use App\Imports\VouchersImport;
use App\Http\Controllers\Controller;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\Input;
use Illuminate\Http\Request;
use DB;
class VouchersController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
//Show data
public function vcrdata()
{
$vouchers = Vouchers::orderby('id_voucher', 'DESC')->get();
$voucherbelumdibayar = Vouchers::where('status', 'Belum dibayar')->get();
$voucheravailable = Vouchers::where('status', 'Available')->get();
$vouchersudahdigunakan = Vouchers::where('status', 'Sudah digunakan')->get();
$vouchertotalbelumdibayar = Vouchers::where('status', 'Belum dibayar')->get('voucher_value');
$batchsummary = Vouchers::select('batch', DB::raw('count(*) as batch_count'), DB::raw('sum(voucher_value) as batch_sum'))->where('status', 'Available')->groupBy('batch')->get();
$monthsummary = Vouchers::select('received_date', DB::raw('count(*) as month_count'), DB::raw('sum(voucher_value) as month_sum'), DB::raw('EXTRACT(MONTH from received_date) as month'), DB::raw('EXTRACT(YEAR from received_date) as year'))->where('status', 'Sudah digunakan')->groupBy('month', 'year')->get();
return view('Pages.Voucher.vcrdata', compact('vouchers', 'voucherbelumdibayar', 'voucheravailable', 'vouchersudahdigunakan', 'vouchertotalbelumdibayar', 'batchsummary', 'monthsummary'));
}
public function exportvouchers()
{
return Excel::download(new VouchersExport, 'exportvouchers.xlsx');
}
public function importvouchers(Request $request)
{
$file = $request->file('file');
$namaFile = $file->getClientOriginalName();
$file->move('DataVouchers', $namaFile);
Excel::import(new VouchersImport, public_path('/DataVouchers/' . $namaFile));
return redirect('vcrdata')->with('toast_success', 'All good!');
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function vcrused($id)
{
$vouchers = Vouchers::findorfail($id);
$stores = Stores::all();
return view('Pages.Voucher.vcrused', compact('vouchers', 'stores'));
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function vcrupdate(Request $request, $id)
{
$vouchers = Vouchers::findorfail($id);
$vouchers->update($request->all());
return redirect('vcrdata')->with('toast_success', 'All good!');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function vcrdelete($id)
{
$vouchers = Vouchers::findorfail($id);
$vouchers = Vouchers::where('id_voucher', $id)->delete();
return redirect('vcrdata')->with('toast_success', 'All good!');
}
}