KTV/app/Livewire/Admin/ArtistImportData.php

61 lines
1.4 KiB
PHP

<?php
namespace App\Livewire\Admin;
use Illuminate\Support\Facades\Auth;
use Livewire\Component;
use Livewire\WithFileUploads;
use App\Jobs\ImportArtistJob;
use Illuminate\Support\Facades\Storage;
class ArtistImportData extends Component
{
use WithFileUploads;
protected $listeners = ['openModal','closeModal'];
public bool $canCreate;
public bool $showModal = false;
public $file;
public function mount()
{
$this->canCreate = Auth::user()?->can('song-edit') ?? false;
}
public function openModal()
{
$this->showModal = true;
}
public function closeModal()
{
$this->showModal = false;
}
public function import()
{
// 檢查檔案是否有上傳
$this->validate([
'file' => 'required|file|mimes:csv,xlsx,xls'
]);
if ($this->canCreate) {
// 儲存檔案至 storage
$path = $this->file->storeAs('imports', uniqid() . '_' . $this->file->getClientOriginalName());
// 丟到 queue 執行
ImportArtistJob::dispatch($path);
$this->reset('file');
$this->showModal = false;
session()->flash('message', '已排入背景匯入作業,請稍候查看結果');
}
}
public function render()
{
return view('livewire.admin.artist-import-data');
}
}