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'); }}
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners