Single Image Upload Using Vue.js & Laravel
Uploading images is a common requirement in modern web applications. In this guide, we’ll build a simple image upload feature using Vue.js as the front end and Laravel as the back end.
1. Set Up Laravel Backend
First, create a new Laravel project if you haven’t already:
Install Laravel CORS & Storage Link
Enable CORS to allow Vue.js to interact with Laravel:
Create a Migration for Image Uploads
Run the following command to create an images
table:
Modify the migration file in database/migrations/xxxx_xx_xx_xxxxxx_create_images_table.php
:
Run the migration:
2. Build the Image Upload API in Laravel
Create a Controller
Modify app/Http/Controllers/ImageUploadController.php
:
Set Up Routes in Laravel
Modify routes/api.php
:
3. Set Up Vue.js Frontend
Install Vue in Laravel
Inside your Laravel project, install Vue and dependencies:
Create a Vue Component for Image Upload
Inside resources/js/components/ImageUpload.vue
:
4. Register Vue Component in Laravel
Modify resources/js/app.js
:
5. Add Vue Component to Laravel Blade File
Modify resources/views/welcome.blade.php
:
6. Start the Laravel & Vue Application
Run Laravel backend:
Run Vue frontend:
7. Test the Image Upload Functionality
- Open
http://127.0.0.1:8000
in your browser. - Select an image and click Upload.
- You should see the uploaded image preview and success message.
Conclusion
✅ Laravel handles file storage securely.
✅ Vue.js provides a smooth frontend experience.
✅ Axios helps communicate between Vue & Laravel.
Now you have a fully functional image upload feature using Vue.js & Laravel! 🚀 Let me know if you need any modifications. 😊