How to Upload User Profile Image in Laravel 12 (Beginner-Friendly Guide)
๐ Introduction: Profile Images Matter!
Whether you're building a social network, an admin panel, or a blogging platform โ allowing users to upload profile images makes your app more personal, interactive, and professional.
Luckily, Laravel 12 makes this task simple and elegant. ๐งโ๐ป
In this tutorial, weโll walk you through step-by-step how to upload a userโs profile image and store it properly in your Laravel app โ without confusion or complexity. Even if you're new to Laravel, youโll be able to follow along easily. ๐
๐งฑ Requirements Before We Begin
Make sure you have the following set up:
- Laravel 12 project installed โ
- A working user registration/login system (e.g. via Laravel Breeze, Jetstream, or custom) โ
- Basic knowledge of Laravel Blade, routes, and controllers โ
If not, you can install Laravel quickly using:
composer create-project laravel/laravel laravel-profile-upload
๐๏ธ Step 1: Add Profile Image Column in Users Table
First, letโs modify the users table to store the profile image path.
๐ง Create a migration:
php artisan make:migration add_profile_image_to_users_table --table=users
โ๏ธ In the migration file:
public function up(): void
{
Schema::table('users', function (Blueprint $table) {
$table->string('profile_image')->nullable()->after('email');
});
}
โ Then run the migration:
php artisan migrate
๐ผ๏ธ Step 2: Update the User Model
Add profile_image to the $fillable array in app/Models/User.php:
protected $fillable = [
'name',
'email',
'password',
'profile_image', // โ
Add this
];
๐ Step 3: Create the Upload Form
Now letโs build a Blade form where users can upload their profile picture.
๐ In your Blade view (e.g., resources/views/profile.blade.php):
<form action="{{ route('profile.upload') }}" method="POST" enctype="multipart/form-data">
@csrf
<div>
<label for="profile_image">Choose Profile Image</label>
<input type="file" name="profile_image" id="profile_image">
</div>
<button type="submit">Upload</button>
</form>
๐ก Tip: Always set enctype="multipart/form-data" for file uploads.
๐ง Step 4: Create the Route
Open routes/web.php and define the upload route:
use App\Http\Controllers\ProfileController;
Route::post('/profile/upload', [ProfileController::class, 'upload'])->name('profile.upload');
๐งโ๐ซ Step 5: Build the Controller Logic
Generate the controller (if you donโt have one already):
php artisan make:controller ProfileController
Now add the upload method to handle the file upload:
๐ฆ app/Http/Controllers/ProfileController.php
public function upload(Request $request)
{
$request->validate([
'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif|max:2048',
]);
$user = auth()->user();
if ($request->hasFile('profile_image')) {
$image = $request->file('profile_image');
$name = time().'_'.$image->getClientOriginalName();
$path = $image->storeAs('profile_images', $name, 'public');
// Optional: delete old image
if ($user->profile_image) {
Storage::disk('public')->delete($user->profile_image);
}
$user->profile_image = $path;
$user->save();
}
return back()->with('success', 'Profile image uploaded successfully!');
}
๐ This saves the image to storage/app/public/profile_images.
๐ Make sure you have a symbolic link:
php artisan storage:link
๐ Step 6: Display the Profile Image
๐ฅ๏ธ Show image in Blade:
@if (auth()->user()->profile_image)
<img src="{{ asset('storage/' . auth()->user()->profile_image) }}" alt="Profile Image" width="150">
@else
<img src="{{ asset('default-avatar.png') }}" alt="Default Image" width="150">
@endif
๐ก Real-World Example Analogy
Think of your Laravel app as a digital passport system.
Uploading a profile image is like pasting a photo on your passport. It helps the system โ and others โ recognize and personalize your experience.
Just like in real life, you need a clear and secure way to store and show that photo โ and Laravel provides all the tools you need!
๐งผ Optional: Clean Code Tips
- Store uploaded images in separate folders per user (
profile_images/user_123/) - Resize/compress images using Intervention Image
- Always validate and sanitize files before saving
โ Key Takeaways
- Use
multipart/form-dataand LaravelโsstoreAs()method to save files securely - Always validate file types and sizes
- Store file paths in the database, not the actual images
- Use
php artisan storage:linkto access images publicly - Show uploaded images using
asset('storage/โฆ')
๐ Conclusion
And there you go โ youโve just learned how to upload and display user profile images in Laravel 12 step-by-step! ๐ฅณ
This is an essential feature for any modern web app and opens the door to even more advanced media handling.
Got stuck somewhere? Let us know in the comments or reach out โ happy coding! ๐จโ๐ป๐ฉโ๐ป
Projects to explore next
Source-code projects from the KritiMyantra catalogue. Check each project's details before buying.
Flask Authentication System (Free Version) - Kritim Yantra
๐ Featuresโ Google & GitHub Authenticationโ User Registration & Loginโ Profile/Dashboard Accessโ Secure & Mi...
Flask Blog CMS with Admin Dashboard โ Python Source Code
A Flask-based blogging website with a built-in admin dashboard. Manage articles, images, categories, tags and featured p...
Laravel 12 + ReactJS Starter Kit: Add Multi-Language Support (Arabic, English, Spanish) with Blog CRUD
๐ Installation & Setup Download & Extract Get the ZIP from kritimyantra.com and extract it. Includes preconfigu...
Comments
No comments yet. Be the first to comment.
Sign in to join the discussion.