要在 Livewire 內上傳檔案,要先讀入 WithFileUploads。
use Livewire\WithFileUploads; class UploadPhoto extends Component { use WithFileUploads; public $photo; public function save() { $this->validate([ 'photo' => 'image|max:1024', // 1MB Max ]); $this->photo->store('photos'); } }
接著準備 HTML 如下:
<form wire:submit.prevent="save">
<input type="file" wire:model="photo">
@error('photo') <span class="error">{{ $message }}</span> @enderror
<button type="submit">Save Photo</button>
</form>
這樣就可以做好透過 Livewire 上傳檔案後驗證還有儲存檔案了。
參考:
File Uploads