One of the things that I see many people struggling with is file uploads. How do we upload a file in Laravel?
it('can upload an avatar', function () { Storage::fake('avatars'); $file = UploadedFile::fake()->image('avatar.jpg'); post( action(UploadController::class), [ 'file' => $file, ], )->assertRedirect(); Storage::disk('avatars')->assertExists($file->hashName()); }); We are faking the storage facade, creating a fake file to upload, and then hitting our endpoint and sending the file.
class UploadService implements UploadServiceContract { public function __construct( private readonly UploadContract $avatar, ) {} public function avatar(UploadedFile $file): File { return $this->avatar->handle( file: $file, ); }}
final class UploadForm extends Component { use WithFileUploads; public null|string|TemporaryUploadedFile $file = null; public function upload() { $this->validate(); } public function render(): View { return view('livewire.upload-form'); }}