feat: export news data
This commit is contained in:
parent
3c1212d2e9
commit
5b8d2d8c67
96
app/Exports/NewsExport.php
Normal file
96
app/Exports/NewsExport.php
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Exports;
|
||||||
|
|
||||||
|
use App\Models\News;
|
||||||
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||||
|
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||||
|
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||||
|
|
||||||
|
class NewsExport implements FromCollection, WithHeadings, WithStyles, WithMapping
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @return \Illuminate\Support\Collection
|
||||||
|
*/
|
||||||
|
|
||||||
|
protected $category;
|
||||||
|
protected $year;
|
||||||
|
protected $month;
|
||||||
|
protected $date;
|
||||||
|
|
||||||
|
public function __construct($category, $year, $month, $date)
|
||||||
|
{
|
||||||
|
$this->category = $category;
|
||||||
|
$this->year = $year;
|
||||||
|
$this->month = $month;
|
||||||
|
$this->date = $date;
|
||||||
|
}
|
||||||
|
public function collection()
|
||||||
|
{
|
||||||
|
$query = News::query()->select(['id', 'title', 'category', 'created_at']);
|
||||||
|
|
||||||
|
if($this->category !== 'all'){
|
||||||
|
if($this->category === 'news'){
|
||||||
|
$query->where('category', '0');
|
||||||
|
}else if($this->category === 'announcement'){
|
||||||
|
$query->where('category', '1');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->year) {
|
||||||
|
$query->whereYear('created_at', $this->year);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->month) {
|
||||||
|
$monthYear = Carbon::parse($this->month);
|
||||||
|
$query->whereYear('created_at', $monthYear->year)
|
||||||
|
->whereMonth('created_at', $monthYear->month);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($this->date) {
|
||||||
|
$query->whereDate('created_at', $this->date);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function map($news): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
$news->id,
|
||||||
|
$news->title,
|
||||||
|
$news->category === '0' ? 'Berita' : 'Pengumuman',
|
||||||
|
Carbon::parse($news->created_at)->locale('id')->translatedFormat('l, d F Y')
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function headings(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
['Data Berita', '', '', ''],
|
||||||
|
['NO', 'Judul', 'Kategori', 'Dibuat Pada']
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public function styles(Worksheet $sheet)
|
||||||
|
{
|
||||||
|
$sheet->mergeCells('A1:D1'); // A1 sampai D1 digabung
|
||||||
|
|
||||||
|
// Set alignment untuk pusat horizontal dan vertikal
|
||||||
|
$sheet->getStyle('A1:D1')->getAlignment()->setHorizontal('center');
|
||||||
|
$sheet->getStyle('A1:D1')->getAlignment()->setVertical('center');
|
||||||
|
|
||||||
|
$sheet->getColumnDimension('A')->setWidth(7); // Set column A width
|
||||||
|
$sheet->getColumnDimension('B')->setAutoSize(true);
|
||||||
|
$sheet->getColumnDimension('C')->setWidth(15); // Set column C width
|
||||||
|
$sheet->getColumnDimension('D')->setWidth(25); // Set column D width
|
||||||
|
|
||||||
|
// Menambahkan gaya lain jika diperlukan
|
||||||
|
$sheet->getStyle('A1:D1')->getFont()->setBold(true);
|
||||||
|
$sheet->getStyle('A1:D1')->getFont()->setSize(14);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,12 +2,14 @@
|
|||||||
|
|
||||||
namespace App\Http\Controllers\Dashboard;
|
namespace App\Http\Controllers\Dashboard;
|
||||||
|
|
||||||
|
use App\Exports\NewsExport;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\NewsRequest;
|
use App\Http\Requests\NewsRequest;
|
||||||
use App\Models\News;
|
use App\Models\News;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
|
use Maatwebsite\Excel\Facades\Excel;
|
||||||
|
|
||||||
class NewsController extends Controller
|
class NewsController extends Controller
|
||||||
{
|
{
|
||||||
@ -103,6 +105,16 @@ public function destroy($id){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function export(Request $request)
|
||||||
|
{
|
||||||
|
$category = $request->input('category', 'all');
|
||||||
|
$year = $request->input('year', null);
|
||||||
|
$month = $request->input('month', null);
|
||||||
|
$date = $request->input('date', null);
|
||||||
|
|
||||||
|
return Excel::download(new NewsExport($category, $year, $month, $date), 'news.xlsx');
|
||||||
|
}
|
||||||
|
|
||||||
public function filter(Request $request){
|
public function filter(Request $request){
|
||||||
|
|
||||||
$filteredRequest = $request->except('_token');
|
$filteredRequest = $request->except('_token');
|
||||||
|
|||||||
@ -12,6 +12,7 @@
|
|||||||
"php": "^8.2",
|
"php": "^8.2",
|
||||||
"laravel/framework": "^11.31",
|
"laravel/framework": "^11.31",
|
||||||
"laravel/tinker": "^2.9",
|
"laravel/tinker": "^2.9",
|
||||||
|
"maatwebsite/excel": "^3.1",
|
||||||
"spatie/laravel-settings": "^3.4",
|
"spatie/laravel-settings": "^3.4",
|
||||||
"sqids/sqids": "^0.5.0",
|
"sqids/sqids": "^0.5.0",
|
||||||
"vinkla/hashids": "^12.0",
|
"vinkla/hashids": "^12.0",
|
||||||
|
|||||||
926
composer.lock
generated
926
composer.lock
generated
File diff suppressed because it is too large
Load Diff
@ -21,6 +21,11 @@
|
|||||||
<a href="javascript: void(0);" class="btn btn-white btn-dim btn-outline-light" data-bs-toggle="modal" data-bs-target="#modalFilter"><em class="d-none d-sm-inline icon ni ni-filter"></em><span><span class="d-none d-md-inline">Filter</a>
|
<a href="javascript: void(0);" class="btn btn-white btn-dim btn-outline-light" data-bs-toggle="modal" data-bs-target="#modalFilter"><em class="d-none d-sm-inline icon ni ni-filter"></em><span><span class="d-none d-md-inline">Filter</a>
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
|
<li>
|
||||||
|
<div class="drodown">
|
||||||
|
<a href="javascript: void(0);" class="btn btn-info" data-bs-toggle="modal" data-bs-target="#modalExport"><em class="d-none d-sm-inline icon ni ni-upload"></em><span><span class="d-none d-md-inline">Export</a>
|
||||||
|
</div>
|
||||||
|
</li>
|
||||||
<li class="nk-block-tools-opt"><a href="{{route('dashboard.news.create')}}" class="btn btn-primary"><em class="icon ni ni-plus"></em><span>Tambah</span></a></li>
|
<li class="nk-block-tools-opt"><a href="{{route('dashboard.news.create')}}" class="btn btn-primary"><em class="icon ni ni-plus"></em><span>Tambah</span></a></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
@ -48,6 +53,56 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="modal fade" id="modalExport">
|
||||||
|
<div class="modal-dialog" role="document">
|
||||||
|
<div class="modal-content">
|
||||||
|
<div class="modal-header">
|
||||||
|
<h5 class="modal-title">Export</h5>
|
||||||
|
<a href="javascript:void(0);" class="close" data-bs-dismiss="modal" aria-label="Close">
|
||||||
|
<em class="icon ni ni-cross"></em>
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
<form action="{{route('dashboard.news.export')}}" class="form-validate is-alter" method="post">
|
||||||
|
@csrf
|
||||||
|
<div class="modal-body">
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label">Kategori</label>
|
||||||
|
<div class="form-control-wrap">
|
||||||
|
<select class="form-select js-select2" name="category">
|
||||||
|
<option value="all">Semua Kategori</option>
|
||||||
|
<option value="news">Berita</option>
|
||||||
|
<option value="announcement">Pengumuman</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label" for="year">Tahun</label>
|
||||||
|
<div class="form-control-wrap">
|
||||||
|
<input type="number" class="form-control" id="year" placeholder="Data berita pada tahun" name="year">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label" for="month">Bulan</label>
|
||||||
|
<div class="form-control-wrap">
|
||||||
|
<input type="month" class="form-control" id="month" placeholder="Data berita pada bulan dan tahun" name="month">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="form-group">
|
||||||
|
<label class="form-label" for="date">Tanggal</label>
|
||||||
|
<div class="form-control-wrap">
|
||||||
|
<input type="date" class="form-control" id="date" placeholder="Data berita pada bulan dan tahun" name="date">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="modal-footer bg-light">
|
||||||
|
<span class="sub-text">
|
||||||
|
<button type="submit" class="btn btn-sm btn-primary">Export</button>
|
||||||
|
</span>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
<div class="modal fade" id="modalFilter">
|
<div class="modal fade" id="modalFilter">
|
||||||
<div class="modal-dialog" role="document">
|
<div class="modal-dialog" role="document">
|
||||||
<div class="modal-content">
|
<div class="modal-content">
|
||||||
|
|||||||
@ -147,6 +147,7 @@
|
|||||||
Route::prefix('news')->name('news.')->group(function(){
|
Route::prefix('news')->name('news.')->group(function(){
|
||||||
Route::controller(NewsController::class)->group(function(){
|
Route::controller(NewsController::class)->group(function(){
|
||||||
Route::get('/', 'index')->name('index');
|
Route::get('/', 'index')->name('index');
|
||||||
|
Route::post('/export', 'export')->name('export');
|
||||||
Route::post('/filter', 'filter')->name('filter');
|
Route::post('/filter', 'filter')->name('filter');
|
||||||
Route::get('/{id}/show', 'show')->name('show');
|
Route::get('/{id}/show', 'show')->name('show');
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user