Dalam tutorial ini, kita akan mencoba Export Excel di Laravel dan Contoh Data CSV menggunakan package maatwebsite/excel. Dalam proyek Laravel kali ini, kita selalu perlu mengimpor atau mengekspor baris dari database. Maatwebsite/excel membuatnya sangat mudah untuk mengimpor-ekspor data di Laravel ke dalam format Excel.MAri ikuti langkah berikut
Memerlukan paket berikut di composer.json dari proyek Laravel. Perintah berikut akan mengunduh paket dan PhpSpreadsheet.
composer require maatwebsite/excel
Maatwebsite\Excel\ExcelServiceProvider adalah auto-ditemukan dan terdaftar secara default.
Jika Anda ingin mendaftar sendiri, tambahkan ServiceProvider di config/app.php:
'providers' => [
/*
* Package Service Providers...
*/
Maatwebsite\Excel\ExcelServiceProvider::class,
]
Jika Anda ingin menambahkannya secara manual, tambahkan Facade di config/app.php:
'aliases' => [
...
'Excel' => Maatwebsite\Excel\Facades\Excel::class,
]
Jika Anda ingin menerbitkan konfigurasi, jalankan perintah vendor publish:
php artisan vendor:publish --provider="Maatwebsite\Excel\ExcelServiceProvider"
Ketik perintah berikut.
php artisan make:model Persib -m
Sekarang, buka file [timestamp] .create_persibes_table.php dan tambahkan kolom.
public function up()
{
Schema::create('persibes', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('show_name');
$table->string('position');
$table->string('number');
$table->timestamps();
});
}
Sekarang, migrasikan database menggunakan perintah berikut.
php artisan migrate
Langkah selanjutnya adalah membuat file PersibController.php .
php artisan make:controller PersibController
Sekarang, tambahkan dua route di dalam file routes >> web.php .
// web.php
Route::get('persib', 'PersibController@create')->name('persib.create');
Route::post('persib', 'PersibController@store')->name('persib.store');
Sekarang, buat dua metode di dalam file PersibController.php .
<?php
// PersibController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Persib;
class PersibController extends Controller
{
public function create()
{
}
public function store()
{
}
}
Sekarang, di dalam folder views , buat satu file bernama file form.blade.php . Tambahkan kode berikut.
@extends('layout')
@section('content')
<style>
.uper {
margin-top: 40px;
}
</style>
<div class="card uper">
<div class="card-header">
Add Persib Shows
</div>
<div class="card-body">
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div><br />
@endif
<form method="post" action="{{ route('persib.store') }}">
<div class="form-group">
@csrf
<label for="name">Name:</label>
<input type="text" class="form-control" name="show_name"/>
</div>
<div class="form-group">
<label for="price">Position :</label>
<input type="text" class="form-control" name="position"/>
</div>
<div class="form-group">
<label for="quantity">Show Number :</label>
<input type="text" class="form-control" name="number"/>
</div>
<button type="submit" class="btn btn-primary">Create Show</button>
</form>
</div>
</div>
@endsection
Sebelum kita membuat file view, kita perlu menambahkan satu route di dalam web.php.
// web.php
Route::get('persib/list', 'PersibController@index')->name('persib.index');
Sekarang, buat file view bernama file list.blade.php . Tambahkan kode berikut.
@extends('layout')
@section('content')
<table class="table table-striped">
<thead>
<th>ID</th>
<th>Name</th>
<th>Position</th>
<th>Number</th>
<th>Action</th>
</thead>
<tbody>
@foreach($shows as $show)
<tr>
<td>{{$show->id}}</td>
<td>{{$show->show_name}}</td>
<td>{{$show->position}}</td>
<td>{{$show->number}}</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
Sekarang, tambahkan kode di dalam fungsi index ( ) dari file PersibController.php .
public function index()
{
$shows = Persib::all();
return view('list', compact('shows'));
}
php artisan make:export PersibExport --model=Persib
File tersebut dapat ditemukan di app/Exportsdirektori.
File PersibExport.php adalah sebagai berikut.
<?php
namespace App\Exports;
use App\Persib;
use Maatwebsite\Excel\Concerns\FromCollection;
class PersibExport implements FromCollection
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return Persib::all();
}
}
Jika Anda lebih suka membuat ekspor secara manual, Anda dapat membuat yang berikut ini di app/Exports.
Di dalam file PersibController.php , tambahkan kode berikut.
// PersibController.php
use App\Persib;
use App\Exports\PersibExport;
use Maatwebsite\Excel\Facades\Excel;
public function export()
{
return Excel::download(new PersibExport, 'persib.xlsx');
}
Jadi, file terakhir kita terlihat seperti di bawah ini.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Persib;
use App\Exports\PersibExport;
use Maatwebsite\Excel\Facades\Excel;
class PersibController extends Controller
{
public function create()
{
return view('form');
}
public function store(Request $request)
{
$validatedData = $request->validate([
'show_name' => 'required|max:255',
'position' => 'required|max:255',
'number' => 'required|max:255',
]);
Persib::create($validatedData);
return redirect('/persib')->with('success', 'Persib Plus Show is successfully saved');
}
public function index()
{
$shows = Persib::all();
return view('list', compact('shows'));
}
public function export()
{
return Excel::download(new PersibExport, 'persib.xlsx');
}
}
Terakhir, tambahkan route untuk dapat mengakses ekspor:
// web.php
Route::get('export', 'PersibController@export');
Juga, tambahkan tautan ke Ekspor di dalam file list.blade.php .
@foreach($shows as $show)
<tr>
<td>{{$show->id}}</td>
<td>{{$show->show_name}}</td>
<td>{{$show->position}}</td>
<td>{{$show->number}}</td>
<td><a href="{{action('PersibController@export')}}">Export</a></td>
</tr>
@endforeach
Secara default, format ekspor ditentukan oleh ekstensi file.
public function export()
{
return Excel::download(new PersibExport, 'persib.csv');
}
Sangat tertarik dengan dunia Pemrograman Web & Mobile, saat ini fokus pada bagian Backend Web Developer, menggunakan PHP sebagai bahasa pemrograman utama, biasanya saya menggunakan Laravel.