Laravel

Best Parctise Import Excel dengan Laravel

Selamat siang para pembaca, udah lama tidak posting. Kali ini saya akan membagikan cara singkat upload data dari excel ke database. Saya menganggap para pembaca disini sudah paham akan konsep Laravel jadi tidak saya jelaskan detailnya.

Pertama buat Controller Import seperti ini bawah ini :

public function ImportData(Request $request){
        try {
            $this->validate($request, [
                'file' => 'required|mimes:csv,xls,xlsx',
                'tahun' => 'required',
            ]);
            $semester = $request->input('semester');
            $tahun = $request->input('tahun');
            $file = $request->file('file');
            $raw = Excel::toArray([], $file);
            $slice = array_slice($raw[0], 2);
            $data = array_map(function($item) use ($tahun, $semester) {
                return [
                    'tanggal' => date('Y-m-d'),
                    'nik' => $item[1],
                    'nama' => $item[2],
                    'unit' => $item[3],
                    'jabatan' => $item[4],
                    'nilai' => $item[5],
                    'semester' => $semester,
                    'tahun' => $tahun,
                ];
            }, $slice);
            $sqlDel =DB::table('table_data')
                            ->where('semester', $semester)
                            ->where('tahun', $tahun)
                            ->delete();
            DB::table('table_nilai_ukk')->insert($data);
                Alert::toast('Data Berhasil Diimport!', 'success');
                return redirect()->back();
        } catch (\Exception $e) {
            Alert::toast("Data Gagal Diimport!\n" . $e->getMessage(), 'danger');
            return redirect()->back();
        }
    }

Selanjuntnya buat bladenya

<!-- modal -->
<div class="modal fade" id="import" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">IMPORT DATA</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
                @csrf
                <div class="modal-body">
                    <div class="form-group">
                        <label for="semester1">SEMESTER</label>
                        <select name="semester" id="semester1" class="form-control" required>
                            <option value="">PILIH</option>
                            <option value="1">SEMESTER 1</option>
                            <option value="2">SEMESTER 2</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <label for="tahun1">TAHUN</label>
                        <select name="tahun" id="tahun1" class="form-control" required>
                            <option value="">PILIH</option>
                            @foreach ($list_tahun as $item)
                            <option value="{{ $item }}" {{ $item === date('Y') ? 'selected' : '' }}>{{ $item }}</option>
                            @endforeach
                        </select>
                    </div>
                    <div class="form-group">
                        <label>PILIH FILE</label>
                        <input type="file" name="file" class="form-control" required>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">TUTUP</button>
                    <button type="submit" class="btn btn-success">IMPORT</button>
                </div>
            </form>
        </div>
    </div>
</div>

Terakhir buat Routenya

Route::post('/import', 'MasterController@nilaiUkkImport')->name('import');

Selesai, mudah bukan ?
Silahkan para pembaca bisa mencobanya, terimakasih..

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button