50 lines
1.4 KiB
PHP
50 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire;
|
|
|
|
use Livewire\Component;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class DropdownExport extends Component
|
|
{
|
|
public $loading = false;
|
|
public $url;
|
|
public $defaultFilter = [];
|
|
|
|
public function mount($url, $defaultFilter = [])
|
|
{
|
|
$this->url = $url;
|
|
$this->defaultFilter = $defaultFilter;
|
|
}
|
|
|
|
public function handleExportCSV()
|
|
{
|
|
$this->loading = true;
|
|
|
|
try {
|
|
// Merge query parameters with default filters
|
|
$params = array_merge(request()->query(), $this->defaultFilter);
|
|
$response = Http::withOptions(['response_type' => 'blob'])->get($this->url['path'], $params);
|
|
|
|
if ($response->successful()) {
|
|
$dateNow = now()->format('mdY');
|
|
$fileName = "{$this->url['fileName']}_{$dateNow}.csv";
|
|
Storage::disk('local')->put($fileName, $response->body());
|
|
return response()->download(storage_path("app/{$fileName}"))->deleteFileAfterSend(true);
|
|
} else {
|
|
session()->flash('error', 'Failed to export CSV.');
|
|
}
|
|
} catch (\Exception $e) {
|
|
session()->flash('error', 'An error occurred: ' . $e->getMessage());
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.dropdown-export');
|
|
}
|
|
}
|