You are on page 1of 4

Catatan Laravel

Ekspor ke excel
Instal dulu
composer require maatwebsite/excel

Deklarasikan package dg menambahkan serviceprovider di config/app.php


'providers' => [
...
Maatwebsite\Excel\ExcelServiceProvider::class,
]

Juga pada bagian aliases


'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]

Kemudian ketikkan perintah berikut


php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"

jika terjadi error, pada composer.json ubah versi maatwebsite/excel menjadi ^3.1 kemudian jalankan
perintah
composer update
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"

kemudian buat file export


php artisan make:export SiswaExport --model=Siswa

hasilnya akan seperti ini:


<?php

namespace App\Exports;

use App\Siswa;
use Maatwebsite\Excel\Concerns\FromCollection;

class SiswaExport implements FromCollection


{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return Siswa::all();
}
}

Buat dua buah route (jika belum ada)


Route::get('/siswa', 'SiswaController@index');
Route::get('/siswa/export_excel', 'SiswaController@export_excel');

Buat controllernya (jika belum ada)


php artisan make:controller SiswaController

kemudian ketikkan
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Siswa;

use App\Exports\SiswaExport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;

class SiswaController extends Controller


{
public function index()
{
$siswa = Siswa::all();
return view('siswa',['siswa'=>$siswa]);
}

public function export_excel()


{
return Excel::download(new SiswaExport, 'siswa.xlsx');
}
}

Buat tampilan utama: resources/views/siswa.blade.php


<!DOCTYPE html>
<html>
<head>
<title>Export Laporan Excel Pada Laravel</title>
<link rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous">
</head>
<body>

<div class="container">
<center>
<h4>Export Laporan Excel Pada Laravel</h4>
<h5><a target="_blank"
href="https://www.malasngoding.com/">www.malasngoding.com</a></h5>
</center>

<a href="/siswa/export_excel" class="btn btn-success my-3"


target="_blank">EXPORT EXCEL</a>

<table class='table table-bordered'>


<thead>
<tr>
<th>No</th>
<th>Nama</th>
<th>NIS</th>
<th>Alamat</th>
</tr>
</thead>
<tbody>
@php $i=1 @endphp
@foreach($siswa as $s)
<tr>
<td>{{ $i++ }}</td>
<td>{{$s->nama}}</td>
<td>{{$s->nis}}</td>
<td>{{$s->alamat}}</td>
</tr>
@endforeach
</tbody>
</table>
</div>

</body>
</html>

Import ke Excel
Buat file import
php artisan make:import SiswaImport --model=Siswa

edit file app/Imports/SiswaImport.php


<?php

namespace App\Imports;

use App\Siswa;
use Maatwebsite\Excel\Concerns\ToModel;

class SiswaImport implements ToModel


{
/**
* @param array $row
*
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function model(array $row)
{
return new Siswa([
'nama' => $row[1],
'nis' => $row[2],
'alamat' => $row[3],
]);
}
}

You might also like