Skip to content

Commit

Permalink
Merge pull request #11 from Innovix-Matrix-Systems/develop
Browse files Browse the repository at this point in the history
Export Functionality Added
  • Loading branch information
AHS12 authored Oct 14, 2023
2 parents daf501e + 546dac4 commit 99fe989
Show file tree
Hide file tree
Showing 11 changed files with 1,107 additions and 254 deletions.
38 changes: 38 additions & 0 deletions app/Exports/BulkExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Exports;

use Illuminate\Contracts\Support\Responsable;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Excel;

class BulkExport implements FromCollection, Responsable
{
use Exportable;

private Collection $records;

private $fileName = 'users.csv';

private $writerType = Excel::CSV;

private $headers = [
'Content-Type' => 'text/csv',
];

public function __construct(Collection $records, $name)
{
$this->records = $records;
$this->fileName = $name;
}

/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return $this->records;
}
}
17 changes: 17 additions & 0 deletions app/Exports/UsersExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Exports;

use App\Models\User;
use Maatwebsite\Excel\Concerns\FromCollection;

class UsersExport implements FromCollection
{
/**
* @return \Illuminate\Support\Collection
*/
public function collection()
{
return User::all();
}
}
15 changes: 11 additions & 4 deletions app/Filament/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Filament\Resources;

use App\Exports\BulkExport;
use App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource\Pages\CreateUser;
use App\Models\User;
Expand All @@ -14,6 +15,7 @@
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Filters\Filter;
use Filament\Tables\Filters\SelectFilter;
use Filament\Tables\Table;
Expand Down Expand Up @@ -176,13 +178,18 @@ public static function table(Table $table): Table
])
->bulkActions([
Tables\Actions\BulkActionGroup::make([
Tables\Actions\DeleteBulkAction::make()
Tables\Actions\DeleteBulkAction::make(),
BulkAction::make('bulk-export')
->label(__('resources.export.bulk'))
->icon('heroicon-o-arrow-down-tray')
->action(fn ($records) => new BulkExport($records, 'users.csv'))
->deselectRecordsAfterCompletion()
])
])

// ->checkIfRecordIsSelectableUsing(
// fn (User $record): bool => !$record->isSuperAdmin() && Auth::user()->hasPermissionTo('user.delete'),
// ); //performance issue
// ->checkIfRecordIsSelectableUsing(
// fn (User $record): bool => !$record->isSuperAdmin() && Auth::user()->hasPermissionTo('user.delete'),
// ); //performance issue
// ->defaultPaginationPageOption(25)
//->reorderable('sort')
->queryStringIdentifier('users')
Expand Down
18 changes: 18 additions & 0 deletions app/Filament/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace App\Filament\Resources\UserResource\Pages;

use App\Exports\UsersExport;
use App\Filament\Resources\UserResource;
use Filament\Actions;
use Filament\Actions\Action;
use Filament\Resources\Pages\ListRecords;
use Maatwebsite\Excel\Facades\Excel;

class ListUsers extends ListRecords
{
Expand All @@ -13,8 +16,23 @@ class ListUsers extends ListRecords
protected function getHeaderActions(): array
{
return [
$this->getExportAction(),
Actions\CreateAction::make(),
];
}

private function getExportAction()
{
return Action::make('export')
->label(__('resources.user.export.all'))
->icon('heroicon-o-arrow-down-on-square-stack')
->action('exportAllUsers')
->color('success');
}

public function exportAllUsers()
{
return Excel::download(new UsersExport, 'users.csv');
}

}
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.2",
"laravel/tinker": "^2.8",
"maatwebsite/excel": "^3.1",
"phpoffice/phpspreadsheet": "^1.29",
"spatie/laravel-permission": "^5.11"
},
"require-dev": {
Expand Down
Loading

0 comments on commit 99fe989

Please sign in to comment.